jee之servlet学习笔记
1.Servlet概述
答: 详见下图:
2.Tomcat容器等级
答: 详见下图:
3.使用Myeclipse编写Servlet
答:详见下图:如果表单提交时用的是get请求,那么我们就重写doGet()方法!
(1)在jsp页面使用get方式或post方式提交数据给后台servlet
(2)后台的servlet
package 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 HelloServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public HelloServlet() {
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 {
System.out.println("处理Get请求...");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(""-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("");
out.println("
A Servlet");out.println("
");out.println("Hello Servlet!
");
out.println(" ");
out.println("");
out.flush();
out.close();
}
/**
* 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 {
System.out.println("处理Post请求...");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(""-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("");
out.println("
A Servlet");out.println("
");out.println("Hello Servlet!
");
out.println(" ");
out.println("");
out.flush();
out.close();
}
/**
* Initialization of the servlet.
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
(3)注册servlet
HelloServlet
/servlet/HelloServlet
index.jsp
记得:myeclipse会自动生成;
localhost是服务器主机名,也可以是IP地址127.0.0.1;8080是tomcat服务器的端口号;helloapp是web工程的上下文地址ContexRoot(一般情况下与web工程名一致);最后是标签中的内容。
4.Servlet执行流程和声明周期
答:详见下图:
servlet生命周期阶段包括初始化、加载、实例化、服务和销毁。
编写Servlet的doPost方法时,需要抛出ServletExcpetion和IOException异常。
5.Tomcat装载Servlet的三种情况
答:详见下图:记住servlet是长期服务器保存到内存中
(1)随便写一个jsp页面提交你数据给servlet
Servlet生命周期
(2)要处理的Servlet
package 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 TestServlet1 extends HttpServlet {
/**
* Constructor of the object.
*/
public TestServlet1() {
System.out.println("TestServlet1构造方法被执行....");
}
/**
* Destruction of the servlet.
*/
public void destroy() {
System.out.println("TestServlet1销毁方法被执行....");
}
/**
* 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 {
System.out.println("TestServlet1的doGet()方法被执行...");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println(""-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("");
out.println("
A Servlet");out.println("
");out.println("
大家好,我是TestServlet1!
");out.println(" ");
out.println("");
out.flush();
out.close();
}
/**
* 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 {
System.out.println("TestServlet1的doPost()方法被执行...");
doGet(request,response); //让doPost()执行与doGet()相同的操作。
}
/**
* Initialization of the servlet.
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
System.out.println("TestServlet1的初始化方法被执行....");
}
}
(3)注册Servlet
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">
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
TestServlet2
/servlet/TestServlet2
index.jsp
6.Servlet与JSP内置对象的对应关系
答:详见下图:
7.Servlet获取表单数据
答
从客户端获取表单的信息:
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("isAccpet")!=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
}
}
8.Servlet路径跳转:
答:详见下图:
(1)访问那个路径下的servlet:
Servlet路径跳转
(2)在Servlet里面使用重定向或服务器转发
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//请求重定向方式跳转到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);
}