Servlet是一种服务器端的Java应用程序,具有独立于平台和协议的特性,可以生成动态的Web页面。
它担当客户请求(Web浏览器或其他HTTP客户程序)与服务器响应(HTTP服务器上的数据库或应用程序)的中间层。
Servlet是位于Web服务器内部的服务器端的Java应用程序,与传统的从命令行启动的Java应用程序不同。
Servlet由Web服务器进行加载,该Web服务器必须包含支持Servlet的Java虚拟机。
Servlet实例化过程
1. servlet容器负责创建servlet的一个实例(在第一次请求servlet的时候).
2. 容器调用该实例的init()方法完成初始化工作.
3. 如果容器对该servlet有请求,则调用此实例的service()方法,service()方法根据请求类型(get还是post)决定调用doXXX()方法.
4. 当web应用被终止时,容器在销毁本实例前调用它的destroy()方法.
5. 销毁并标记该实例以供作为垃圾收集.
在servlet生命周期中,servlet的初始化和和销毁阶段只会发生一次。
而service方法执行的次数则取决于servlet被客户端访问的次数。
Servlet如何处理请求
当用户发送一个请求到某个Servlet的时候,Servlet容器会创建一个ServletRequst和ServletResponse对象.
在ServletRequst对象中封装了用户的请求信息,然后Servlet容器把创建好的ServletRequst和ServletResponse对象传给用户所请求的Servlet,Servlet把处理好的结果写在ServletResponse中,最后Servlet容器把响应结果传给用户。
一个简单的Servlet
package com.app.servlet;
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 Jue extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html; charset=utf-8");
PrintWriter out = resp.getWriter();
out.print("这是一个get");
out.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html; charset=utf-8");
PrintWriter out = resp.getWriter();
out.print("这是一个post");
out.close();
}
@Override
public void destroy() {
// TODO Auto-generated method stub
super.destroy();
}
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
}
}
配置Servlet的web.xml
servlet和servlet-mapping中的servlet-name要保持一致。
url-pattern即是该servlet的访问路径。
<!--servlet逻辑名-->
<servlet>
<servlet-name>Jue</servlet-name>
<servlet-class>com.app.servlet.Jue</servlet-class>
</servlet>
<!--servlet映射-->
<servlet-mapping>
<servlet-name>Jue</servlet-name>
<url-pattern>/jue.eee</url-pattern>
</servlet-mapping>
Servlet的get访问
Servlet默认的即为get访问。
在地址栏输入:http://localhost:8888/WebDemo/jue.eee
Servlet的post访问
Servlet的post访问必须建立在JSP的表单的基础上。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'MyJsp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page. <br>
<form method="post" action="user.eee">
<table cellspacing="5" align="center" width="840" border-collapse="collapse">
<tr><td width="220" align="right"><img src="img/logo_login01.gif"/></tr>
<tr ><td align="center"></td><td></td></tr>
<tr ><td align="right">账 号: </td>
<td><input type="text"id="username" name="username"style="height:25px;width:250px" vspace="5"></td></tr>
<tr><td align="right">密 码: </td>
<td><input id="password"type="password" name="password"style="height:25px;width:250px" vspace="5"></td></tr>
<tr><td ></td><td><input type="checkbox">
<font size="1" face="微软雅黑" color="red"> 记住我一周</font></td>
<tr><td ></td><td>
<input type="submit" name="submit" value="LOGIN"/>
<a href="https://passport.csdn.net/account/forgotpassword" target="_blank" align="right">
<font size="1"><u>忘记密码</u></font></a>
</td></tr>
</table>
</form>
</body>
</html>
访问该表单所在页面:http://localhost:8888/WebDemo/MyJsp.jsp
登录后,页面即会跳转至 Servlet 的post页面 :http://localhost:8888/WebDemo/user.eee