使用HttpServlet完成一个假登录

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
    评论
是,我们可以使用继承HttpServlet的方式来创建一个Servlet。 HttpServlet是Servlet API中的一个抽象类,它实现了javax.servlet.Servlet接口和javax.servlet.http.HttpServlet接口。通过继承HttpServlet类,我们可以方便地创建一个Servlet,并重写其中的doGet()、doPost()等方法来处理客户端的请求。 例如,下面的代码演示了如何使用继承HttpServlet的方式来创建一个Servlet: ``` public class HelloServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 处理HTTP GET请求的代码 } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 处理HTTP POST请求的代码 } } ``` 在上面的代码中,我们创建了一个名为HelloServlet的Servlet,并重写了doGet()和doPost()方法来处理HTTP GET和POST请求。在这些方法中,我们可以通过HttpServletRequest对象获取请求参数、请求头等信息,并使用HttpServletResponse对象向客户端浏览器发送响应数据。 需要注意的是,继承HttpServlet的方式虽然可以方便地创建Servlet,但也增加了类的层级关系和代码的复杂度。在实际开发中,应根据具体业务需求选择合适的方式来创建Servlet,以达到最佳的代码可读性和维护性。 因此,我们可以使用继承HttpServlet的方式来创建一个Servlet,并在其中重写doGet()、doPost()等方法来处理客户端的请求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值