使用HttpServlet完成一个假登录

首先创建一个web项目,然后配置tomcat服务器,在创建好的项目下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>
        <servlet-name>login</servlet-name>
        <servlet-class>com.yang.servlet.login</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>login</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>

</web-app>

然后在创建一个login.jsp的项目,写入代码如下

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

在index.jsp的项目下写入代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>第一个web项目</title>
  </head>
  <body>
  <h1>这是第一个web项目</h1>
  <a href="login.jsp">login</a>
  <a href="zhuce">zhuce</a>
  </body>
</html>

创建一个java项目,写入代码

package com.yang.servlet;

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

public class login extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       System.out.println("Login-get...");
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Login-post...");


        String url=request.getRequestURI();
        String contextPath=request.getContextPath();
        String servletPath=request.getServletPath();

        System.out.println(url);
        System.out.println(contextPath);
        System.out.println(servletPath);

        request.setCharacterEncoding("utf-8");
        String user = request.getParameter("user");
        String pwd = request.getParameter("pwd");

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

        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        if (user.equals("梨子")&&pwd.equals("666")){
            response.getWriter().write("<h2>登录成功!</h2>");
        }else {
            response.getWriter().write("<h2>登录失败!</h2>");
        }
    }
}

运行一下,得到以下结果

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是,我们可以使用继承HttpServlet的方式来创建一个ServletHttpServletServlet 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请求的代码 } } ``` 在上面的代码中,我们创建了一个名为HelloServletServlet,并重写了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、付费专栏及课程。

余额充值