用servlet进行用户名和密码校验

登录界面

输入用户名及密码过后的界面

 

项目目录结构

 

登录界面代码

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
	body{
		margin:0;
	}
	input{
		margin:30px auto;
		width:200px;
		height:28px;
	}
	.border{
		width:400px;
		height:300px;
		background:url(./img/bg.jpg) no-repeat;
		margin:200px auto;
	}
	.content{
		width:300px;
		height:58px;
		border-radius:4px;
		margin: 30px auto;
		color:#fff;
	}
	.commit{
		position:absolute;
		margin-top:30px;
		left:760px;
		background:#3385ff;
	}
	.btnSubmit{
		line-height:30px;
		font-size:16px;
		width:100px;
		background:#3385ff;
		border:0;
	}
</style>
</head>
<body>
	<div class="border">
		<form action="./login" method="POST">
			<div class="content">
				用户名:<input type="text" name="username" placeholder="请输入用户名"><br/>
			</div>
			<div class="content">
				密   码:<input type="password" name="password" placeholder="请输入密码"><br/>
			</div>
			<div class="commit">
				<button type="submit" class="btnSubmit" style="">提交</button>
			</div>
			
	</form>
	</div>
	
	
</body>
</html>

LoginServlet.java

package com.my.deal;

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

/**
 * 登录校验
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
   
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("你好,欢迎:"+username+",来到首页");
        response.getWriter().write("<br/>");
        response.getWriter().write("你的密码是:"+password);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>12</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>login.html</welcome-file>
  </welcome-file-list>
  <servlet>
      <servlet-name>LoginServlet</servlet-name>
      <servlet-class>com.my.deal.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>LoginServlet</servlet-name>
      <url-pattern>/login</url-pattern>
  </servlet-mapping>
</web-app>

总结

通过小demo实验过后,让自己更加清楚的了解Servlet的执行顺序。

 源码下载地址

链接: https://pan.baidu.com/s/1VUPKvVSBv-wckCAy4Mm1Kg

提取码: guew 

posted @ 2019-03-28 20:37  QAQqiulin  阅读(509)  评论(0编辑  收藏  举报