#Servlet是什么?
Servlet是利用Java类编写的服务端应用程序,它可以被看作是位于客户端和服务器的一个中间层,负责接受和请求客户端用户的响应。
1:对客户端发送的数据惊醒读取和拦截
2:读取客户端请求的隐含数据
3:运行结果或者生活结果
4:发送响应的数据
#Servlet的技术特点
1:高效率
2:简单方便
#Servlet的生命周期
// 初始化
public void init() throws ServletException {
super.init();
}
// 运行
protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
super.service(arg0, arg1);
}
// 销毁
public void destroy() {
super.destroy();
}
#Servlet的xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>org.ws.test.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
#index文件
<body>
<form action="login" method="post">
用户名:<input type="text" name ="name"><br/>
密码:<input type="password" name ="pwd"><br/>
<input type="submit" value="登录">
</form>
</body>
#LogIn文件
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String name =req.getParameter("name");
String pwd =req.getParameter("pwd");
System.out.println(" 姓名:"+name+"/密码:"+pwd);
System.out.println("get.......");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String name =req.getParameter("name");
String pwd =req.getParameter("pwd");
System.out.println(" 姓名:"+name+"/密码:"+pwd);
System.out.println("post.......");
}
}
#在浏览器页面输入,在控制台显示即可