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<rvlet-name>
<servlet-class>org.cgq.servlet.LoginServlet<rvlet-class>
<rvlet>
<servlet-mapping>
<servlet-name>login<rvlet-name>
<url-pattern>login</url-pattern>
<rvlet-mapping>
</web-app>
index文件
<body>
<form action="test" method="post">
用户名:<input type="text" name="name1"/><br/>
密码:<input type="password" name="pwd1"><br/>
<input type="submit" value="登录">
</form>
</body>
LogIn文件
package org.sunjinglan.test;
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 Servlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String name= req.getParameter("name1");
String pwd=req.getParameter("pwd1");
System.out.println("姓名:"+name+"密码:"+pwd);
System.out.println("get...");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String name= req.getParameter("name1");
String pwd=req.getParameter("pwd1");
System.out.println("姓名:"+name+"密码:"+pwd);
System.out.println("post...");
}
}
最后在浏览器上运行就好