阅读 136

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初始网页

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


Hello World!

  然后是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.RequestDemo01class>
    
    
        requsetDemo01
        /demo01
    

 

原文:https://www.cnblogs.com/Sum-muji/p/15081078.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐