荐 JavaWeb之Servlet(二)Servlet生命周期方法与GET/POST请求分发
导航
·JavaWeb之Servlet(一)Maven创建Servlet项目时的换源与运行时404的可能解决方案
·JavaWeb之Servlet(二)Servlet生命周期方法与GET/POST请求分发
·JavaWeb之Servlet(三)ServletConfig与ServletContext
·JavaWeb之Servlet(四)HttpServletRequest与HttpServletResponse
前言
项目结构
一、Servlet生命周期方法
只调用一次
- 执行Servlet构造器方法
- 执行init初始化方法
每次访问服务器时调用
- 执行service方法
关闭时调用
- 执行destroy方法
使用IDEA快捷键Alt+Insert创建以下方法
package com.HolaAs;
import javax.servlet.*;
import java.io.IOException;
public class testServlet implements Servlet {
public testServlet() {
System.out.println("First:Construction");
}
@Override
public void init(ServletConfig servletConfig) throws ServletException{
System.out.println("Second:Initialization");
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("Third:Access");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
System.out.println("Forth:Destruction");
}
}
浏览器访问testServlet,Server命令台有回显:
二、Servlet请求分发
Ⅰ、尝试GET和POST请求
在webapp文件夹创建一个提交页面,action指向url-pattern
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>This is for test</title>
</head>
<body>
<form action="http://localhost:8080/javaWeb_Repository_war_exploded/testServlet" method="get">
<input type="submit">
</form>
</body>
</html>
访问http://localhost:8080/javaWeb_Repository_war_exploded/forTest.html
提交表单,网页定位到http://localhost:8080/javaWeb_Repository_war_exploded/testServlet?
访问了testServlet程序,服务器命令台有回显
而method改成post时,与get的效果一致
由于get和post是两种方法,我们希望把二者分开,即分发请求
Ⅱ、请求分发
修改service方法
通过多态的向下转型,将ServletRequest转型成HttpServletRequest
HttpServletRequest类提供了getmethod()可以得到请求类型
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("Third:Access");
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
String method = httpServletRequest.getMethod();
if (method.equals("GET")){
System.out.println("method == GET");
}else if(method.equals("POST")){
System.out.println("method == POST");
}
}
验证分发成功
最终封装修改程序为:
package com.HolaAs;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
public class testServlet implements Servlet {
public testServlet() {
System.out.println("First:Construction");
}
@Override
public void init(ServletConfig servletConfig) throws ServletException{
System.out.println("Second:Initialization");
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("Third:Access");
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
String method = httpServletRequest.getMethod();
if (method.equals("GET")){
doGet();
}else if(method.equals("POST")){
doPost();
}
}
public void doGet(){
System.out.println("method == GET");
}
public void doPost(){
System.out.println("method == POST");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
System.out.println("Forth:Destruction");
}
}
三、实际开发时通过继承HttpServlet实现Servlet程序
程序代码:
package com.HolaAs;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class testServlet2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doGet from testServlet2");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doPost from testServlet2");
}
}
再次提交表单观察命令台
成功分发
完
欢迎在评论区留言
感谢浏览
本文地址:https://blog.csdn.net/Xxy605/article/details/107249213
上一篇: C#异步下载文件
下一篇: C#实现简单过滤非法字符实例