JavaEE MVC学生登陆源代码.docx
- 文档编号:3516785
- 上传时间:2022-11-23
- 格式:DOCX
- 页数:23
- 大小:20.05KB
JavaEE MVC学生登陆源代码.docx
《JavaEE MVC学生登陆源代码.docx》由会员分享,可在线阅读,更多相关《JavaEE MVC学生登陆源代码.docx(23页珍藏版)》请在冰豆网上搜索。
JavaEEMVC学生登陆源代码
Controller
packagecn.controller;
importjava.io.IOException;
importjavax.servlet.ServletException;
importjavax.servlet.annotation.WebServlet;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importcn.bean.StudentDetail;
importcn.util.DBUtil;
/**
*ServletimplementationclassController
*/
@WebServlet("/ctrl")
publicclassControllerextendsHttpServlet{
privatestaticfinallongserialVersionUID=1L;
/**
*@seeHttpServlet#HttpServlet()
*/
publicController(){
super();
}
/**
*@seeHttpServlet#doGet(HttpServletRequestrequest,HttpServletResponse
*response)
*/
protectedvoiddoGet(HttpServletRequestrequest,
HttpServletResponseresponse)throwsServletException,IOException{
doPost(request,response);
}
/**
*@seeHttpServlet#doPost(HttpServletRequestrequest,HttpServletResponse
*response)
*/
protectedvoiddoPost(HttpServletRequestrequest,
HttpServletResponseresponse)throwsServletException,IOException{
Stringaction=request.getParameter("action");
Stringforword="show.jsp";
DBUtildbUtil=newDBUtil();
try{
if(action==null||"show".equals(action)){
request.setAttribute("stus",dbUtil.getAllList());
}elseif("login".equals(action)){
Stringuser=request.getParameter("user");
Stringpassword=request.getParameter("password");
Stringpass=dbUtil.getPassword(user);
if(pass==null){
forword="login.jsp";
request.getSession().setAttribute("errMsg","账号不存在");
}elseif(pass.equals(password)){
request.getSession().setAttribute("login",true);
forword="ctrl";
}else{
forword="login.jsp";
request.getSession().setAttribute("errMsg","账号密码不正确");
}
response.sendRedirect(forword);
return;
}elseif("add".equals(action)){
forword="edit.jsp";
}elseif("save".equals(action)){
forword="show.jsp";
System.out.println(request.getParameter("stuName"));
StringoldStuNum=(String)request.getParameter("oldStuNum");
StudentDetailstu=newStudentDetail();
stu.setStuNum(request.getParameter("stuNum"));
stu.setStuName(request.getParameter("stuName"));
stu.setAge(Integer.parseInt(request.getParameter("age")));
stu.setSex(request.getParameter("sex"));
stu.setStuClass(request.getParameter("stuClass"));
stu.setHomeAddress(request.getParameter("homeAddress"));
stu.setPostCode(request.getParameter("postCode"));
System.out.println(stu.toString());
if(oldStuNum!
=null&&oldStuNum!
=""){
dbUtil.update(stu,oldStuNum);
}else{
dbUtil.add(stu);
}
request.setAttribute("stus",dbUtil.getAllList());
}elseif("update".equals(action)){
StringstuNum=request.getParameter("stuNum");
StudentDetailstu=dbUtil.getStuDetail(stuNum);
request.setAttribute("stu",stu);
forword="edit.jsp";
}elseif("delete".equals(action)){
StringstuNum=request.getParameter("stuNum");
dbUtil.delete(stuNum);
request.setAttribute("stus",dbUtil.getAllList());
}elseif("addUser".equals(action)){
StringuserName=request.getParameter("user");
Stringpassword=request.getParameter("password");
Stringpassword2=request.getParameter("password2");
forword="login.jsp";
if(userName==null||password==null||password2==null){
forword="register.jsp";
request.getSession().setAttribute("errMsg","信息未填写完整");
}
elseif(userName.length()>12||password.length()>12){
forword="register.jsp";
request.getSession().setAttribute("errMsg","账号密码过长");
}elseif(!
password.equals(password2)){
forword="register.jsp";
request.getSession().setAttribute("errMsg","两次输入密码不一致");
}else{
Stringpass=dbUtil.getPassword(userName);
if(pass!
=null){
forword="register.jsp";
request.getSession().setAttribute("errMsg","该用户名已经存在");
}else{
dbUtil.addUser(userName,password);
request.getSession().setAttribute("errMsg","注册成功");
}
response.sendRedirect(forword);
return;
}
}else{
thrownewException("指定操作不存在");
}
}catch(Exceptione){
e.printStackTrace();
request.setAttribute("errMsg",e.getStackTrace());
forword="error.jsp";
}
request.getRequestDispatcher(forword).forward(request,response);
}
}
CheckRight
packagecn.filter;
importjava.io.IOException;
importjavax.servlet.Filter;
importjavax.servlet.FilterChain;
importjavax.servlet.FilterConfig;
importjavax.servlet.ServletException;
importjavax.servlet.ServletRequest;
importjavax.servlet.ServletResponse;
importjavax.servlet.annotation.WebFilter;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;
/**
*ServletFilterimplementationclassCheckRight
*/
@WebFilter("/ctrl")
publicclassCheckRightimplementsFilter{
/**
*Defaultconstructor.
*/
publicCheckRight(){
//TODOAuto-generatedconstructorstub
}
/**
*@seeFilter#destroy()
*/
publicvoiddestroy(){
//TODOAuto-generatedmethodstub
}
/**
*@seeFilter#doFilter(ServletRequest,ServletResponse,FilterChain)
*/
publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,
FilterChainchain)throwsIOException,ServletException{
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
HttpServletRequestreq=(HttpServletRequest)request;
HttpServletResponseres=(HttpServletResponse)response;
HttpSessionsession=req.getSession();
Objectlogin=session.getAttribute("login");
Stringaction=req.getParameter("action");
request.setAttribute("right",login!
=null);
if("edit,delete,insert".contains(action==null?
"xx":
action)
&&login==null){
res.sendRedirect("login.jsp");
}else
chain.doFilter(request,response);
}
/**
*@seeFilter#init(FilterConfig)
*/
publicvoidinit(FilterConfigfConfig)throwsServletException{
//TODOAuto-generatedmethodstub
}
}
Encoding
packagecn.filter;
importjava.io.IOException;
importjavax.servlet.Filter;
importjavax.servlet.FilterChain;
importjavax.servlet.FilterConfig;
importjavax.servlet.ServletException;
importjavax.servlet.ServletRequest;
importjavax.servlet.ServletResponse;
importjavax.servlet.annotation.WebFilter;
/**
*ServletFilterimplementationclassEncoding
*/
@WebFilter("/*")
publicclassEncodingimplementsFilter{
/**
*Defaultconstructor.
*/
publicEncoding(){
}
/**
*@seeFilter#destroy()
*/
publicvoiddestroy(){
}
/**
*@seeFilter#doFilter(ServletRequest,ServletResponse,FilterChain)
*/
publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,
FilterChainchain)throwsIOException,ServletException{
//placeyourcodehere
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//passtherequestalongthefilterchain
chain.doFilter(request,response);
}
/**
*@seeFilter#init(FilterConfig)
*/
publicvoidinit(FilterConfigfConfig)throwsServletException{
}
}
DBUtil
packagecn.util;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
importjava.util.ArrayList;
importcn.bean.StudentBean;
importcn.bean.StudentDetail;
publicclassDBUtil{
staticStringlistSQL="select*fromstudent";
staticStringdetailSQL="select*fromstudentwherestuID=?
";
staticStringinsertSQL="insertintostudent(stuID,stuName,sex,age,stuClass,homeAddress,postCode)values(?
?
?
?
?
?
?
)";
staticStringupdateSQL="updatestudentsetstuID=?
stuName=?
sex=?
age=?
stuClass=?
homeAddress=?
postCode=?
wherestuID=?
";
staticStringdeleteSQL="deletefromstudentwherestuID=?
";
staticStringuserPasswordSQL="selectuser,passwordfromuserwhereuser=?
";
staticStringDBDriver="com.mysql.jdbc.Driver";
staticStringdbURL="jdbc:
mysql:
//localhost:
3306/student?
characterEncoding=UTF-8";
staticStringdbUser="root";
staticStringdbPasswd="115314";
staticStringgetUserAmountSQL="selectcount(pk)fromuser";
staticStringaddUserSQL="insertintouser(pk,user,password)values(?
?
?
)";
privateConnectioncon;
privatevoidgetConnection()throwsClassNotFoundException,SQLException{
Class.forName(DBDriver);
con=DriverManager.getConnection(dbURL,dbUser,dbPasswd);
}
privateStatementgetStatement()throwsClassNotFoundException,
SQLException{
if(con==null){
getConnection();
}
returncon.createStatement();
}
privatePreparedStatementgetPStat(Stringsql)
throwsClassNotFoundException,SQLException{
if(con==null){
getConnection();
}
returncon.prepareStatement(sql);
}
publicArrayList
ClassNotFoundException{
ResultSetrs=getStatement().executeQuery(listSQL);
ArrayList
while(rs.next()){
StudentBeanstu=newStudentBean();
stu.setStuNum(rs.getString("stuID"));
stu.setStuName(rs.getString("stuName"));
stu.setSex(rs.getString("sex"));
stu.setStuClass(rs.getString("stuClass"));
stus.add(stu);
}
rs.close();
if(con!
=null){
con.close();
}
returnstus;
}
publicStudentDetailgetStuDetail(Stringxh)throwsClassNotFoundException,
SQLException{
StudentDetailstu=null;
PreparedStatementpstat=getPStat(detailSQL);
pstat.setString(1,xh);
ResultSetrs=pstat.executeQuery();
if(r
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JavaEE MVC学生登陆源代码 MVC 学生 登陆 源代码
![提示](https://static.bdocx.com/images/bang_tan.gif)