使用HttpServlet完成一个假登录

创建了两个HTML页面,一个用于展示登录和注册链接,另一个处理登录表单提交。使用Servlet进行后端逻辑处理,通过web.xml配置Servlet映射,并在Java类中检查用户名和密码是否匹配,以实现登录功能。
摘要由CSDN通过智能技术生成

1,先写一个页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>页面</title>
  </head>
  <body>
  <h2>页面</h2>
  <a href="denglu.jsp">登录</a>
  <a href="zhuce.jsp">注册</a>
  </body>
</html>

如图所示:

 2,再写一个点击登录跳转

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
<body>
<h2>登录</h2>
<form method="post" action="denglu">
    账号:<input name="user" type="text" value=""><br>
    密码:<input name="pwd" type="password" value=""><br>
    <input type="submit" value="登录">
</form>
</body>
</html>

如图所示:

 3,再写一个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">
    <servlet>
        <servlet-name>denglu</servlet-name>
        <servlet-class>com.w.Servle.denglu</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>denglu</servlet-name>
        <url-pattern>/denglu</url-pattern>
    </servlet-mapping>
</web-app>

4,最后创建一个java类用来继承HttpServlet

package com.w.Servle;

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 denglu extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        String contextPath=req.getParameter("user");
        String pwd=req.getParameter("pwd");

        System.out.println(contextPath);
        System.out.println(pwd);

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        if(contextPath.equals("11111")&&pwd.equals("22222")){
            resp.getWriter().write("登录成功");
        }else {
            resp.getWriter().write("登录失败");
        }
    }
}
如图所示:

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值