实验6 Servlet的开发和使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_42623428/article/details/85610284

基于HTML+Servlet开发一个小型的管理学生信息的Web系统,要求:1)服务器端使用文件保存所有的学生信息,包括:学号、姓名、性别、所在学院、系统密码;2)提供学生登录的界面,可以获取客户端发送的学生学号和密码,并判断学号和密码是否正确,并最终在浏览器中打印是否验证通过的信息提示:如果学号和密码输入正确,则显示学生的学号、姓名、性别、所在学院,否则提示用户名、密码有误。


import java.util.*;
import java.io.*;

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 Student
 */
@WebServlet("/check")
public class Student extends HttpServlet {
	class user{
		public String name;
		public String no;
		public String xueyuan;
		public String sex;
		public String password;
		public user(String no,String name,String sex,String xueyuan,String password){
			this.no=no;
			this.name=name;
			this.xueyuan=xueyuan;
			this.sex =sex;
			this.password=password;
		}
		@Override
		public String toString() {
			return name + " " + no + " " +  sex + " " + xueyuan + " "
					+ password;
		}
	}
	private static final long serialVersionUID = 1L;
    Map<String,user> mp=new HashMap<String,user>();
    /**
     * @throws FileNotFoundException 
     * @see HttpServlet#HttpServlet()
     */
    public Student() throws FileNotFoundException {
        super();
        // TODO Auto-generated constructor stub
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    public void init() {
    	@SuppressWarnings("resource")
		String path=this.getServletContext().getRealPath("/WEB-INF/student.txt");
		InputStreamReader fis = null;
		try {
			fis = new InputStreamReader(new FileInputStream(path));
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
		BufferedReader in=new BufferedReader(fis);
		String a[]=new String[6];
    	String line = null;
    	while(true) {
    		try {
				line=in.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		if(line==null)break;
    		a=line.split("#");
    		user t=new user(a[0],a[1],a[2],a[3],a[4]);
    		mp.put(a[0],t);
    	}
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		//response.setHeader("Content-type", "text/html;charset=UTF-8");  
		String student_name="";
		student_name=request.getParameter("username");
		String student_password=request.getParameter("password");
		String s="";
		if(student_name==null)s+="(请您从‘index.html’文件进入或者在URL处填写参数!)";
		else if(mp.containsKey(student_name)) {
			if(mp.get(student_name).password.equals(student_password)) {
				s+=mp.get(student_name).toString();
			}
			else s+="密码错误";
		}
		else s+="用户名输入错误";
		request.setAttribute("str", s);
		request.getRequestDispatcher("NewFile.jsp").forward(request,response); 
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}
<!doctype html>
<html >
<head>
  <meta charset="utf-8">
  <title>Login</title>
  <link rel="stylesheet" href="css/amazeui.min.css">
  <link rel="stylesheet" href="css/app.css">
</head>
<body background="img/5.jpg"
	style="background-repeat: no-repeat; background-size: cover; background-attachment: fixed;"
	text="#68228B" >
	<div class="am-g myapp-login">
		 <div class="myapp-login-logo">
		 	<i class="am-icon-stumbleupon"></i>
		 </div>
		 <div class="am-u-sm-10 myapp-login-form">
		 	<form class="am-form" action="check" method="POST">
			    <div class="am-form-group">
			      <input type="text"  value="请输入用户名 " name="username">
			    </div>
			    <div class="am-form-group">
			      <input type="password" value="123456" name="password">
			    </div> 
			      <p style="color:green">默认密码为123456</p>
			    <p><button type="submit" class="am-btn am-btn-default">Login</button></p>

			</form>
		 </div>
	</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>message</title>

<style type="text/css">/*P标签样式*/
p {
	text-align: center; /*居中*/
	padding-top: 350px; /*距离顶部300px*/
	font-size: 40px; /*字体大小*/
	color: blue; /*字体颜色*/
}
</style>
</head>
<body background="img/6.jpg"
	style="background-repeat: no-repeat; background-size: cover; background-attachment: fixed;"
	text="#68228B">
	<p><%=request.getAttribute("str")%>
	</p>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_42623428/article/details/85610284