zoukankan      html  css  js  c++  java
  • request (请求转发)

    index.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>登录</title>
    </head>
    <body>
    
    <h1>登录</h1>
    
    <div>
        <%--这里表单的意思是:以post方式提交表单,提交到login请求中--%>
        <from action="${pageContext.request.getContextPath()}/login" method="post">
            用户名:<input type="text" name="username"><br>
            密 码:<input type="text" name="password"><br>
            爱 好:
            <input type="checkbox" name="hobbys" value="女孩">女孩
            <input type="checkbox" name="hobbys" value="代码">代码
            <input type="checkbox" name="hobbys" value="唱歌">唱歌
            <input type="checkbox" name="hobbys" value="电影">电影
    
            <br>
            <input type="submit">
        </from>
    
    
    </div>
    
    </body>
    </html>


    实现类:LoginServlet

    package com.king.servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.util.Arrays;
    
    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[] hobbys = req.getParameterValues("hobbys");
    
            System.out.println("==================================");
            System.out.println(username);
            System.out.println(password);
            System.out.println(Arrays.toString(hobbys));
            System.out.println("==================================");
    
            System.out.println(req.getContextPath());
    
            //通过请求转发
            req.getRequestDispatcher("/success.jsp").forward(req,resp);
    
    
        }
    
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }

    转发成功页面

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <h1>登陆成功</h1>
    
    </body>
    </html>

    配置web 

    <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>login</servlet-name>
            <servlet-class>com.king.servlet.LoginServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>login</servlet-name>
            <url-pattern>/login</url-pattern>
        </servlet-mapping>
    
    </web-app>
  • 相关阅读:
    ThinkDev组件库 开篇
    使用 CodeIgniter 框架快速开发 PHP 应用(六)
    使用 CodeIgniter 框架快速开发 PHP 应用(五)
    jQuery EasyUI 的截图插件(imgAreaSelect)用法
    细说UI线程和Windows消息队列
    ASP.NET MVC2 异常处理机制中令人费解的HTTP 500错误
    从了解到深入——剖析WF4的数据流
    .NET 4.0中数组的新增功能
    成功就是把自己的特长发挥得淋漓尽致(更新)
    WPF4数据绑定应用之“创建具有多种显示效果的字串”
  • 原文地址:https://www.cnblogs.com/CL-King/p/13771329.html
Copyright © 2011-2022 走看看