首页 > 其他分享 >11-登录并转发

11-登录并转发

时间:2022-08-16 21:17:33浏览次数:45  
标签:11 登录 resp req 转发 import servlet javax

11-登录并转发

概述

本文主要讲述一个利用Servlet实现的登录并转发的功能的小例子
几乎当前所有的网站在你用户登录之后,都会给重定向到其他页面

实践

直接上实践了
LoginServlet.java


package com.kuang.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 功能描述
 *
 * @since 2022-08-16
 */
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 4928727484940769433L;

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

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String[] hobbies = req.getParameterValues("hobbies");
        System.out.println(username + ":" + password);
        for (String hobby : hobbies) {
            System.out.println(hobby);
        }

        req.getRequestDispatcher("/success.jsp").forward(req, resp);
    }
}

index.jsp

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

<div>
    <form action=${pageContext.request.contextPath}/login method="post">
        用户名:<input type="text" name="username"> <br>
        密码:<input type="password" name="password"> <br>
        爱好:
        <input type="checkbox" name="hobbies" value="看书">看书
        <input type="checkbox" name="hobbies" value="代码">代码
        <input type="checkbox" name="hobbies" value="电影">电影
        <input type="checkbox" name="hobbies" value="跑步">跑步
        <br>
        <input type="submit">
    </form>
</div>
</body>
</html>

success.jsp

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

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
                      https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
         version="5.0"
         metadata-complete="true">
    <servlet>
        <servlet-name>login</servlet-name>
        <servlet-class>com.kuang.servlet.LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>login</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>
</web-app>

子pom

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

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.example</groupId>
        <artifactId>JavaWeb-Study</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>05-Login</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>05-Login Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
</project>

父pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>JavaWeb-Study</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>01-HelloServlet</module>
        <module>02-ServletContext</module>
        <module>03-Download</module>
        <module>04-VerificationCode</module>
        <module>05-Login</module>
    </modules>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>
    </dependencies>
</project>

标签:11,登录,resp,req,转发,import,servlet,javax
From: https://www.cnblogs.com/Oh-mydream/p/16592972.html

相关文章

  • leetcode1175-质数排列
    质数排列分别找出质数和合数的数量,将两者的阶乘相乘即可classSolution{publicintnumPrimeArrangements(intn){intcnt=0;for(inti=2;......
  • CF1110E Magic Stones
    题目链接:洛谷CodeforcesSolution以前做过这种题,比这题难得多,所以看到就秒了,加强版:[NOIP2021]方差。再来推一遍:如果每次操作\(a_i\),那么差分数组的变化为:\[d_i=a_i......
  • qt5.9 +vs2015 32bit 错误“-1: error: LNK1158: 无法运行“rc.exe”
    开发平台qt5.9.0+vs201532bit....在准备运行vs2015及安装了vs2019后,运行原来可以运行的程序时,出现了错误“-1:error:LNK1158:无法运行“rc.exe”复制了“C:\Progra......
  • 用户信息登录功能
    页面简单优化会做出以下的功能1.简单功能列表查询,登录,添加,删除,修改2.复杂功能删除选中,分页查询,复杂条件查询  <%@pagecontentType=......
  • 11.jmeter实现跨线程调用token值
    1.请求登录接口提取token值,确保token值提取成功,可以添加调试取样器在请求中,查看结果树是否提取成功   2.设置token为全局变量线程组-添加-BeanShell取样器,输入脚本......
  • Taurus.MVC 微服务框架 入门开发教程:项目部署:2、让Kestrel支持绑定多个域名转发,替代Ng
    系列目录:本系列分为项目集成、项目部署、架构演进三个方向,后续会根据情况调整文章目录。本系列第一篇:Taurus.MVCV3.0.3微服务开源框架发布:让.NET架构在大并发的演......
  • 基于C++的OpenGL 11 之投光物
    1.引言本文基于C++语言,描述OpenGL的投光物前置知识可参考:基于C++的OpenGL10之光照贴图-当时明月在曾照彩云归-博客园(cnblogs.com)笔者这里不过多描述每个名......
  • 跳板攻击之:MSF portfwd 端口转发与端口映射
    跳板攻击之:MSFportfwd端口转发与端口映射郑重声明:本笔记编写目的只用于安全知识提升,并与更多人共享安全知识,切勿使用笔记中的技术进行违法活动,利用笔记中的技术造成的......
  • windows11上打开ie11浏览器(亲测有效)
    使用vbs脚本方式第一步:新建txt文件,复制以下代码CreateObject("InternetExplorer.Application").Visible=true保存,修改后缀名为.vbs双击文件,即可打开ie11第二步在edge......
  • 7-11 关键活动
    假定一个工程项目由一组子任务构成,子任务之间有的可以并行执行,有的必须在完成了其它一些子任务后才能执行。“任务调度”包括一组子任务、以及每个子任务可以执行所依赖的......