HttpServletRequest
HttpServletRequest
Request请求和Response响应相对应。
以下是模拟登录然后重定向到特定网页
jsp中:${pageContext.request.contextPath}可以获得网页页面的绝对路径。
首先是:RequestDemo01类
public class RequestDemo01 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext servletContext = this.getServletContext(); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); resp.sendRedirect("/ServletTestDemo_war/TestDemo.jsp"); String username = req.getParameter("username"); String password = req.getParameter("password"); System.out.println(username + "\n" + password); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } }
然后是index.jsp初始网页
然后是TestDemo.jsp测试网页
<%-- Created by IntelliJ IDEA. User: 22729 Date: 2021/7/30 Time: 16:31 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %>TestDemo
最后是ResultDemo.jsp
<%-- Created by IntelliJ IDEA. User: 22729 Date: 2021/7/30 Time: 16:51 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %>Result Succes
web.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0" metadata-complete="true"> requsetDemo01 class>com.zhang.Servlet.RequestDemo01 class>requsetDemo01 /demo01
原文:https://www.cnblogs.com/Sum-muji/p/15081078.html