Servlet本身不能独立运行,需要web应用,而web应用是部署在tomcat中
入门
1、先创建一个web程序
2、导入servlet的jar包
3、配置web.xml
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.llhc.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
4、HelloServlet
package com.llhc;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet{
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
resp.getWriter().println("<h1>Hello Servlet</h1>");
resp.getWriter().println(new Date().toLocaleString());
}
}
5、http://localhost:8080/J2EEDemo/hello
图片.png
加入页面
1、先创建一个web程序
2、导入servlet的jar包
3、配置web.xml
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.llhc.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
4、写一个HelloServlet
package com.llhc;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet{
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name=req.getParameter("name");
String password=req.getParameter("password");
System.out.println("name:"+name);
System.out.println("password:"+password);
}
}
5、加入页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<form action="login" method="post">
账号: <input type="text" name="name"> <br>
密码: <input type="password" name="password"> <br>
<input type="submit" value="登录">
</form>
</body>
</html>
图片.png
返回数据(普通返回)
1、先创建一个web程序
2、导入servlet的jar包
3、配置web.xml
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.llhc.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
4、写一个HelloServlet
package com.llhc;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet{
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name=req.getParameter("name");
String password=req.getParameter("password");
System.out.println("name:"+name);
System.out.println("password:"+password);
String html = "";
if("admin".equals(name) && "123".equals(password)){
html = "<div style='color:green'>success</div>";
}else{
html = "<div style='color:red'>fail</div>";
}
//返回数据
PrintWriter pw=resp.getWriter();
//返回html
pw.println(html);
}
}
5、加入页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<form action="login" method="post">
账号: <input type="text" name="name"> <br>
密码: <input type="password" name="password"> <br>
<input type="submit" value="登录">
</form>
</body>
</html>
图片.png
网友评论