首页 > 其他分享 >转发request的应用

转发request的应用

时间:2023-01-15 13:33:06浏览次数:35  
标签:resp req request jsp 应用 转发 import servlet javax

1.index.jsp代码:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2023-01-15
  Time: 11:43
  To change this template use File | Settings | File Templates.
--%>


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>
        登录
    </title>
</head>
<body>

<div style="text-align: center">
    <%--这里表单表示的意思,以post方式提交表单,提交到我们的login请求--%>
    <%--${pageContext.request.contextPath}代表当前项目--%>
    <form action="${pageContext.request.contextPath}/login" method="post">
        用户名:<input type="text" name="username"><br>
        密码:<input type="password"  name="password"><br>
        爱好:
        <input type="checkbox" name="hobby">女孩</input>
        <input type="checkbox" name="hobby">代码</input>
        <input type="checkbox" name="hobby">唱歌</input>
        <input type="checkbox" name="hobby">电影</input>
        <br>
        <input type="submit">
    </form>
</div>
</body>
</html>

 2.成功提示页面success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>成功!</h2>

</body>
</html>

 3.代码LoginServlet.jsp

package com.yin;

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 LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        String [] hobbies=req.getParameterValues("hobby");
        //req.getParameter("hobby");
        System.out.println("============================");

        System.out.println(username+":"+password);

        System.out.println("============================");

        //通过请求转发,/代表当前的web应用

       req.getRequestDispatcher("/success.jsp").forward(req,resp);
        //req.getRequestDispatcher(req.getContextPath()+"/success.jsp").forward(req,resp);
        resp.setCharacterEncoding("utf-8");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

}

  4.web.xml代码:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         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">


    <servlet>
        <servlet-name>req</servlet-name>
        <servlet-class>com.yin.LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>req</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>

</web-app>

  

标签:resp,req,request,jsp,应用,转发,import,servlet,javax
From: https://www.cnblogs.com/insoon/p/17053370.html

相关文章