关于分级自测题,我们知道该系统一共分为两个角色,一个是读者,一个是管理员,我们需要根据不同用户去到不同的页面,所以我们需要写一个登陆界面。
本文先写这个功能的实现,该功能主要在servlet里面实现。
一.先写jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"//denglu.jsp pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>图书管理系统:登陆界面</title> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <% Object message1 = request.getAttribute("message1");//显示注册成功(下一篇博客) if(message1!=null && !"".equals(message1)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message1")%>"); </script> <%} %> <form action="login_servlet?method=login" method="post"> <table align="center" border="1" width="500"> <tr> <td>账号: </td> <td> <input type="text" name="studentid" id="studentid" /> </td> </tr> <tr> <td>密码: </td> <td> <input type="password" name="password"/> </td> </tr> <tr> <td>身份: </td> <td> <select name="form">//这里传值给servlet <option value="读者"selected>读者</option> <option value="管理员"selected>管理员</option> </select> </td> </tr> <tr align="center"> <td colspan="2"> <input type="submit" value="登录" onclick="return judge()" /> <a href="zhuce.jsp" >点击注册</a> </td> </tr> </table> </body> </html>
reader.jsp//读者功能
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="Bean.*,java.util.ArrayList,util.*,dao.*,servlet.*"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <a href="book_servlet?method=looklist" ><h3>浏览图书信息</h3></a> <a href="goodbook1.jsp" ><h3>查询图书信息</h3></a> <a href="book_servlet?method=delist" ><h3>借阅图书</h3></a> <a href="book_servlet?method=borrowlist&borrower=<%=good.getStudentid()%>"><h3>浏览催还书目</h3></a> <a href="book_servlet?method=cuihuan" ><h3>归还图书</h3></a> </body> </html>
genlist.jsp//管理员功能
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="Bean.*,java.util.ArrayList,util.*,dao.*,servlet.*"%> <html> <head> <meta charset="UTF-8"> <title>管理员界面</title> </head> <body> <a href="addstudent.jsp" ><h3>添加读者信息</h3></a> <a href="addbook.jsp" ><h3>添加新书信息</h3></a> <a href="update.jsp" ><h3>打印催还书信息</h3></a> </body> </html>
二.servlet层
package servlet; 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; import javax.servlet.http.HttpSession; import Bean.xinxi; import dao.dao; import daoimpl.daoimpl; /** * Servlet implementation class login_servlet */ @WebServlet("/login_servlet") public class login_servlet extends HttpServlet { private static final long serialVersionUID = 1L; dao gooddao=new daoimpl(); /** * @see HttpServlet#HttpServlet() */ public login_servlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); String method=request.getParameter("method"); String form=request.getParameter("form");//获取来自denglu.jsp里面的数据if("login".equals(method)) { //通过判断语句进入不同页面if(form.equals("读者"))request.getRequestDispatcher("reader.jsp").forward(request, response); else if(form.equals("管理员"))request.getRequestDispatcher("genlist.jsp").forward(request, response); } }
三.运行结果
这里只是一个前端页面,没有与数据库打交道,下一篇写验证账号密码是否正确,需要实现注册功能,连接数据库。
喜欢就点点关注吧!!!