servlet-保持前端请求方式和后台处理方式方式一致 2
程序员文章站
2022-07-12 15:48:17
...
上一篇文章说到LoginServlet和LogoutServlet的所需要的的请求方式不一样(一个是POST请求,一个是GET请求),为了前后台的一致,需要判断,但是每一个Servlet都要写相同的代码。
有时间可以看看sun公司写的HttpServlet的类,远比下边的这个要详细
我们可以写一个父类,继承GenericServlet,再让LoginServlet和LogServlet继承我们所写的这个父类就好了。
我们起名这个父类就叫HttpServlet
HttpServlet
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HttpServlet extends GenericServlet {
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)res;
service(request,response);
}
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
String method = request.getMethod();
if("GET".equals(method))
{
doGet(request,response);
}else if("POST".contentEquals(method)) {
doPost(request,response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.print("405-您应该发送GET请求");
throw new RuntimeException("405-您应该发送GET请求");
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.print("405-您应该发送POST请求");
throw new RuntimeException("405-您应该发送POST请求");
}
}
LoginServlet
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().print("login....");
}
}
LogoutServlet
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LogoutServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().print("系统正在安全退出");
}
}
html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login page</title>
</head>
<body>
<form action="/08-HttpServlet原理/login" method="get">
username<input type="text" name="username"><br>
password<input type="password" name="password"><br>
<input type="submit" value="login">
</form>
<!-- 安全退出系统 /logout-->
<form action="/08-HttpServlet原理/logout" method="post">
<input type="submit" value="退出系统">
</form>
</body>
</html>
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<welcome-file-list>
<welcome-file>login.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>logout</servlet-name>
<servlet-class>com.servlet.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>logout</servlet-name>
<url-pattern>/logout</url-pattern>
</servlet-mapping>
</web-app>
上一篇: Servlet3.0——异步请求处理
下一篇: 力软敏捷开发框架更新,加入葡萄城报表功能