在src新建TestServlet1:
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class TestServlet1
*/
@WebServlet("/TestServlet1")
public class TestServlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestServlet1() {
System.out.println("TestServlet1构造方法被执行....");
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
System.out.println("TestServlet1初始化方法被执行....");
}
/**
* @see Servlet#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
System.out.println("TestServlet1销毁方法被执行....");
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("TestServlet1的doGet()方法被执行...");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println(" A Servlet ");
out.println(" ");
out.println("你好,我是TestServlet1!
");
out.println(" ");
out.println("");
out.flush();
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("TestServlet1的doPost()方法被执行...");
doGet(request,response);//让doPost()执行与doGet()相同的操作。
}
}
新建index.jsp:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
Insert title here
Servlet生命周期
以Get方式请求TestServlet1
web.xml:
This is the description of my J2EE component
This is the display name of my J2EE component
TestServlet1
servlet.TestServlet1
This is the description of my J2EE component
This is the display name of my J2EE component
TestServlet2
servlet.TestServlet2
TestServlet1
/servlet/TestServlet1
index.jsp
数字越小表示装在的优先级别越高。
修改web.xml:
This is the description of my J2EE component
This is the display name of my J2EE component
TestServlet1
servlet.TestServlet1
2
This is the description of my J2EE component
This is the display name of my J2EE component
TestServlet2
servlet.TestServlet2
1
TestServlet1
/servlet/TestServlet1
index.jsp
修改TestServlet2.java:
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class TestServlet2
*/
@WebServlet("/TestServlet2")
public class TestServlet2 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestServlet2() {
System.out.println("TestServlet2构造方法被执行....");
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
System.out.println("TestServlet2初始化方法被执行....");
}
/**
* @see Servlet#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
System.out.println("TestServlet2销毁方法被执行....");
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("TestServlet2的doGet()方法被执行...");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println(" A Servlet ");
out.println(" ");
out.println("你好,我是TestServlet2!
");
out.println(" ");
out.println("");
out.flush();
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("TestServlet2的doPost()方法被执行...");
doGet(request,response);//让doPost()执行与doGet()相同的操作。
}
}
注意:这两个servlet实例已经驻留在服务器的内存当中,其实客户端并没有请求这两个servlet,但在服务器启动的时候已被事先装在。
D:\技术\爬虫\java\.metadata\.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps\ServletLifeCycleDemo\WEB-INF\classes\servlet
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class TestServlet1
*/
@WebServlet("/TestServlet1")
public class TestServlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestServlet1() {
System.out.println("TestServlet1构造方法被执行....");
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
System.out.println("TestServlet1初始化方法被执行....");
}
/**
* @see Servlet#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
System.out.println("TestServlet1销毁方法被执行....");
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("TestServlet1的doGet()方法被执行...");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println(" A Servlet ");
out.println(" ");
out.println("大家好,我是TestServlet1!
");
out.println(" ");
out.println("");
out.flush();
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("TestServlet1的doPost()方法被执行...");
doGet(request,response);//让doPost()执行与doGet()相同的操作。
}
}
SimpleDateFormat转换接收到的字符串类型为日期型(也可以将日期型转换为某种格式的字符串)。
parse方法:将指定类型的字符串转换为日期型。
Servlet获取表单数据
reg.jsp:用户注册的静态页面
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'reg.jsp' starting page
用户注册
userinfo.jsp:用户注册成功之后,提交给userinfo显示用户注册成功的用户信息。
<%@ page language="java" import="java.util.*,java.text.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'userinfo.jsp' starting page
用户信息
用户名:
密码:
性别:
E-mail:
出生日期:
<%
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
String date = sdf.format(regUser.getBirthday());
%>
<%=date%>
爱好:
<%
String[] favorites = regUser.getFavorites();
for(String f:favorites)
{
%>
<%=f%>
<%
}
%>
自我介绍:
是否介绍协议:
建一个实体类提交公户注册表单:Users.java(用户实体类)
package entity;
import java.util.Date;
//用户实体类
public class Users {
private String username; //用户名
private String mypassword; //密码
private String email; //电子邮箱
private String gender; //性别
private Date birthday; //出生日期
private String[] favorites; //爱好
private String introduce; //自我介绍
private boolean flag; //是否接受协议
public Users()
{
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getMypassword() {
return mypassword;
}
public void setMypassword(String mypassword) {
this.mypassword = mypassword;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String[] getFavorites() {
return favorites;
}
public void setFavorites(String[] favorites) {
this.favorites = favorites;
}
public String getIntroduce() {
return introduce;
}
public void setIntroduce(String introduce) {
this.introduce = introduce;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
}
处理用户注册的RegServlet类:
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import entity.Users;
public class RegServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public RegServlet() {
super();
}
/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet.
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
Users u = new Users();
String username,mypassword,gender,email,introduce,isAccept;
Date birthday;
String[] favorites;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try
{
username = request.getParameter("username");
mypassword = request.getParameter("mypassword");
gender = request.getParameter("gender");
email = request.getParameter("email");
introduce = request.getParameter("introduce");
birthday = sdf.parse(request.getParameter("birthday"));
if(request.getParameterValues("isAccept")!=null)
{
isAccept = request.getParameter("isAccept");
}
else
{
isAccept = "false";
}
//用来获取多个复选按钮的值
favorites = request.getParameterValues("favorite");
u.setUsername(username);
u.setMypassword(mypassword);
u.setGender(gender);
u.setEmail(email);
u.setFavorites(favorites);
u.setIntroduce(introduce);//封装
if(isAccept.equals("true"))
{
u.setFlag(true);
}
else
{
u.setFlag(false);
}
u.setBirthday(birthday);
//把注册成功的用户对象保存在session中
request.getSession().setAttribute("regUser", u);
//跳转到注册成功页面
request.getRequestDispatcher("../userinfo.jsp").forward(request,response);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
/**
* Initialization of the servlet.
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
getParameterValues接收字符串数组,用来获取多个复选按钮的值。
注:本节课老师打的源代码有错,RegServlet里面的isAccept写成了isAccpet
在页面上调用Servlet时路径的写法
使用相对路径访问HelloServlet(超链接方式):
href="报错
如何解决删除servlet后The servlet name already exists
新建index.jsp:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting page
Servlet路径跳转
访问HelloServlet!
新建HelloServlet.jsp:
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class HelloServlet
*/
@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public HelloServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}
/**
* @see Servlet#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println(" A Servlet ");
out.println(" ");
out.println("您好,我是HelloServlet!
");
out.println(" ");
out.println("");
out.flush();
out.close();
}
}
新建web.xml:
This is the description of my J2EE component
This is the display name of my J2EE component
HelloServlet
servlet.HelloServlet
HelloServlet
/servlet/HelloServlet
index.jsp
结果:
使用绝对路径访问HelloServlet(超链接方式):
index.jsp:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting page
Servlet路径跳转
访问HelloServlet!
访问HelloServlet!
Servlet当中页面跳转问题
采用请求重定向的方式:
新建TestServlet:
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class TestServlet
*/
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}
/**
* @see Servlet#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//请求重定向方式跳转到test.jsp,当前路径是ServletPathDirection/servlet/
//response.sendRedirect("test.jsp");
//使用request.getContextPath()获得上下文对象
response.sendRedirect(request.getContextPath()+"/test.jsp");
}
}
新建test.jsp:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting page
Test.jsp
web.xml:
This is the description of my J2EE component
This is the display name of my J2EE component
HelloServlet
servlet.HelloServlet
This is the description of my J2EE component
This is the display name of my J2EE component
TestServlet
servlet.TestServlet
HelloServlet
/servlet/HelloServlet
TestServlet
/servlet/TestServlet
index.jsp
服务器内部跳转:
TestServlet.jsp:
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class TestServlet
*/
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}
/**
* @see Servlet#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//请求重定向方式跳转到test.jsp,当前路径是ServletPathDirection/servlet/
//response.sendRedirect("test.jsp");
//使用request.getContextPath()获得上下文对象
//response.sendRedirect(request.getContextPath()+"/test.jsp");
//服务器内部跳转,这里的斜线表示项目的根目录
request.getRequestDispatcher("/test.jsp").forward(request, response);
}
}
结果一样
网页获取资源文件方式:
TestServlet:
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class TestServlet
*/
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}
/**
* @see Servlet#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//请求重定向方式跳转到test.jsp,当前路径是ServletPathDirection/servlet/
//response.sendRedirect("test.jsp");
//使用request.getContextPath()获得上下文对象
//response.sendRedirect(request.getContextPath()+"/test.jsp");
//服务器内部跳转,这里的斜线表示项目的根目录
//request.getRequestDispatcher("/test.jsp").forward(request, response);
request.getRequestDispatcher("../test.jsp").forward(request, response);
}
}
结果一样