Web后端开发-模拟用户登录验证(html Servlet)

文章详细描述了如何通过HTML表单和JavaServlet进行用户登录验证,涉及参数传递、服务器端验证及中文乱码处理。
摘要由CSDN通过智能技术生成

Web后端开发-模拟用户登录验证(html Servlet) 

具体要求:

1、登录提交页面login.html :包含两个接受用户名和登录密码的文本框,一个登录的submit按钮;

2、loginServlet程序:实现获取用户名和密码的数据进行模拟登录验证,最后将验证结果返回给客户端。

3、用户名、密码的验证通过与Servlet初始化参数中的数据进行比对。

4、使用注解方式或者使用web.xml配置文件的方式,配置Servlet初始化参数都可以。

注意:

1、服务器返回中文的中文乱码问题;

2、客户端与服务器间使用Post方式传递数据。

代码

login.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login" method="post">
	<table>
		<tr><td>用户名:</td><td><input type="text" name="username"></td></tr>
		<tr><td>密码:</td><td><input type="text" name="password"></td></tr>
		<tr><td><input type="submit"></td><td><input type="reset"></td></tr>
	</table>
</form>
</body>
</html>

LoginServlet.java

package edu.hbun.work1.users;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String username2 = "";
    private String password2 = "";
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		this.username2 = config.getInitParameter("username1");
		this.password2 = config.getInitParameter("password1");
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 使用Post方式传递数据
		String username3 = request.getParameter("username");
		String password3 = request.getParameter("password");
		response.setCharacterEncoding("GBK");//避免中文乱码
		//获取用户名和密码数据进行模拟登录验证
		PrintWriter writer = response.getWriter();
		if(this.username2.equals(username3)&&this.password2.equals(password3)){
			writer.println("登录成功,欢迎:" + username3);
		}else{
			writer.println("用户名或密码错误");
		}
	}

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>WeekDemo2</display-name>
  <welcome-file-list>
    <welcome-file>login.html</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>login1</servlet-name>
    <servlet-class>edu.hbun.work1.users.LoginServlet</servlet-class>
    <init-param>
      <param-name>username1</param-name>
      <param-value>0315</param-value>
    </init-param>
    <init-param>
      <param-name>password1</param-name>
      <param-value>123456</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>login1</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>
</web-app>

结果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值