在线考试系统(3)——学生登录实现

目录

 

1.建立数据库管理层com.java.dao

2.struts的action类

3.struts的配置文件struts.xml

4.登录界面login.jsp


1.建立数据库管理层com.java.dao

package com.java.dao;

import org.hibernate.Query;
import org.hibernate.Session;

import com.java.model.Student;
import com.java.util.HibernateUtil;

public class StudentDao {

	public Student login(Student student)throws Exception{
		//获取当前的session
		Session session=HibernateUtil.getSessionFactory().getCurrentSession();
		//开启事务
		session.beginTransaction();
		//获取query,利用query获取所有信息
		Query query=session.createQuery("from Student as s where s.id=:id and s.password=:password");
		//设置登录的id和password
		query.setString("id", student.getId());
		query.setString("password", student.getPassword());
		//来获取用户登录的信息,包括id和password
		Student resultStu=(Student)query.uniqueResult();
		//提交事务
		session.getTransaction().commit();
		//最后返回用户所填写的登录信息
		return resultStu;
	}
}

 

2.struts的action类

package com.java.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.java.dao.StudentDao;
import com.java.model.Student;
import com.opensymphony.xwork2.ActionSupport;

public class StudentAction extends ActionSupport implements ServletRequestAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//定义request
	private HttpServletRequest request;
	private StudentDao studentDao=new StudentDao();
	
	private Student student;
	//登录时的错误信息
	private String error;
	
	
	
	public Student getStudent() {
		return student;
	}



	public void setStudent(Student student) {
		this.student = student;
	}



	public String getError() {
		return error;
	}



	public void setError(String error) {
		this.error = error;
	}

	//登录方法
	public String login() throws Exception{
		//获取session
		HttpSession session=request.getSession();
		//调用登录方法
		Student currentUser=studentDao.login(student);
		//进行判断
		if(currentUser==null){
			error="准考证号或密码错误";
			return ERROR;
		}else{
			//将正确信息存入session
			session.setAttribute("currentUser", currentUser);
			return SUCCESS;
		}
		
	}


	@Override
	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request=request;
	}

}

 

3.struts的配置文件struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

	<!-- name是给包取名 extends后面继承struts-default-->
	<package name="studentInfo" namespace="/" extends="struts-default">
		
		<action name="student" class="com.java.action.StudentAction">
			<result name="success" type="redirect">/main.jsp</result>
			<result name="error">/login.jsp</result>
		</action>
		
	</package>
	

     
</struts>

 

4.登录界面login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>在线考试系统学生登录界面</title>
<link href="${pageContext.request.contextPath}/style/exam.css" rel="stylesheet">
<link href="${pageContext.request.contextPath}/bootstrap/css/bootstrap.css" rel="stylesheet">
<link href="${pageContext.request.contextPath}/bootstrap/css/bootstrap-responsive.css" rel="stylesheet">
<script src="${pageContext.request.contextPath}/bootstrap/js/jQuery.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap/js/bootstrap.js"></script>
<script type="text/javascript">
		/* 登录按钮功能 */
		function checkForm(){
			/* 获取当前用户输入的id和password */
			var id=document.getElementById("id").value;
			var password=document.getElementById("password").value;
			/* 这里进行判断 */
			if(id==null || id==""){
				alert("准考证号不能为空!");
				return false;
			}
			if(password==null || password==""){
				alert("登录密码不能为空!");
				return false;
			}
			return true;
		}
		/* 重置按钮功能 */
		function resetValue(){
			document.getElementById("id").value="";
			document.getElementById("password").value="";
		}
</script>
</head>
<body>
<div align="center">
	<form action="student!login" method="post" onsubmit="return checkForm()">
		<!-- 根据具体的图片大小尺寸 -->
		<table width="1536" height="735" background="image/login.jpg">
			<tr height="100">
				<td colspan="4"></td>
			</tr>
			<!-- 第一行,准考证号 -->
			<tr height="10">
				<td width=20%></td>
				<td><label style="color: black; text-shadow: 0 0 black; font-size: 18px">准考证号:</label></td>
				<td><input type="text" style="font-size: 18px" id="id" name="student.id" value="${student.id }"/></td>
				<td width=60%></td>
			</tr>
			<!-- 第二行,登录密码 -->
			<tr height="50">
				<td width=20%></td>
				<td><label style="color: black; text-shadow: 0 0 black; font-size: 18px">登录密码:</label></td>
				<td><input type="password" style="font-size: 18px" id="password" name="student.password" value="${student.password }"/></td>
				<td width=60%></td>
			</tr>
			<!-- 第三行,登录密码 -->
			<tr height="50">
				<td width=20%></td>
				<td align="center"><button type="submit" class="btn btn-primary">登录</button></td>
				<td align="center"><button type="button" class="btn btn-primary" onclick="resetValue()">重置</button></td>
				<td width=60%></td>
			</tr>
			<tr height="100">
				<td colspan="4"></td>
			</tr>
		</table>
	</form>
</div>
<!-- 错误信息提示 -->
<script>
	if('${error}'!=''){
		alert('${error}');
	}
</script>
</body>
</html>

这里我们运行一下看看;

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值