1.Servlet 通过调用 init () 方法进行初始化。
2.Servlet 调用 service() 方法来处理客户端的请求。
3.Servlet 通过调用 destroy() 方法终止(结束)。
编写一Servlet程序,要求当访问该Servlet时,在浏览器中显示”helloworld,我的第一个servlet程序!”的字样。
前言
作为个人学习记录,我会尽可能介绍的很详细,有误和不清楚地方可评论指正。
一、Servlet 生命周期
Servlet 生命周期可被定义为从创建直到毁灭的整个过程。以下是 Servlet 遵循的过程:
1.Servlet 通过调用 init () 方法进行初始化。
init 方法被设计成只调用一次。它在第一次创建 Servlet 时被调用,在后续每次用户请求时不再调用。因此,它是用于一次性初始化
2.Servlet 调用 service() 方法来处理客户端的请求。
service() 方法由容器调用,service 方法在适当的时候调用 doGet、doPost、doPut、doDelete 等方法。所以,您不用对 service() 方法做任何动作,您只需要根据来自客户端的请求类型来重载 doGet() 或 doPost() 即可。
3.Servlet 通过调用 destroy() 方法终止(结束)。
destroy() 方法只会被调用一次,在 Servlet 生命周期结束时被调用。Servlet 是由 JVM 的垃圾回收器进行垃圾回收的。
4.doGet() 方法
GET 请求来自于一个 URL 的正常请求,或者来自于一个未指定 METHOD 的 HTML 表单,它由 doGet() 方法处理。
5.doPost() 方法
POST 请求来自于一个特别指定了 METHOD 为 POST 的 HTML 表单,它由 doPost() 方法处理。
二、Servlet实例
编写一Servlet程序,要求当访问该Servlet时,在浏览器中显示”helloworld,我的第一个servlet程序!”的字样。
1.写一个html或者jsp页面来跳转
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>servlet</title>
</head>
<a href="hello">访问servlet</a>
</body>
</html>
2.在java目录下写servlet页面
package com.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/hello")
public class HolloWorld extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter pw=resp.getWriter();
pw.print("<html><head><title>servelt</title></head><body>HolloWorld,我的第一个servlet程序!</body></html>");
pw.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);;
}
@Override
public void destroy() {
System.out.println("开始销毁");
}
@Override
public void init() throws ServletException {
System.out.println("开始运行");
}
}
三、可能出现的问题
1.404找不到servlet
原因:没有设置 servlet路径或者路径不对应,拼写错误
办法:添加路径且与jsp页面设置的相对应
@WebServlet("/xxx")
2.新建类时选择超类时没有HttpServlet类
解决办法:
右键项目--〉java 构建路径--〉添加库(A);选择一个tomcat版本
无法解析类型 javax.servlet.http.Http
四、另外一个例子
编写一Servlet程序完成登录模块的设计与实现,在登录页面login.html输入用户,密码,如果用户名为pyy,密码为123,则显示如下界面:
如果用户名,密码输入错误,则显示如下界面:
代码 :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<form action="loginservlet" method="post">
<strong>用户名</strong>:<input name="username" type="text">
<strong>密 码</strong>:<input name="psw" type="password">
<input type="submit" value="进入">
<input name="Submit" type="reset" value="重置"><br>
</form>
</body>
</html>
“java”目录下com.controller包下loginservlet类
package com.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/loginservlet")
public class loginservlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");//使客户端浏览器区分不同种类的数据,这里设置为text和html类型,且编码方式为...
req.setCharacterEncoding("utf-8");//设置对客户端请求进行重新编码的编码。
String user=req.getParameter("username");//获取从表单传入的参数
String psw=req.getParameter("psw");
PrintWriter out=resp.getWriter();//开启输出流
if(user.equals("彭于晏")&&psw.equals("123")) {//分别打印出想要的页面
out.print("<html><head><title></title></head><body bgcolor='peachpuff'>");
out.print("<h1>你好"+user+"!欢迎登录学生管理系统!</h1><hr /><table><tr>");
out.print("<td>学生姓名</td><td>学生学号</td><td>学生宿舍号</td><td>学生手机号</td></tr>");
out.print("<tr><td>小明</td><td>001</td><td>75118</td><td>19515861230</td></tr>");
out.print("<tr><td>小红</td><td>002</td><td>55213</td><td>19515489652</td></tr>");
out.print("<tr><td>小张</td><td>003</td><td>65479</td><td>19546851238</td></tr>");
out.print("</table></body></html>");
}else{
out.print("<html><title>失败</title><body bgcolor='orange'>");
out.print("<h1>对不起,你好像不是彭于晏!<a href='login.html'>重新登陆</a></h1>");
out.print("</body></html>");
}
out.close();//关闭输出流
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
@Override
public void destroy() {//在控制台显示Servlet的创建与关闭
System.out.println("开始销毁");
}
@Override
public void init() throws ServletException {
System.out.println("开始运行");
}
}