高级网站设计试验报告2.docx
《高级网站设计试验报告2.docx》由会员分享,可在线阅读,更多相关《高级网站设计试验报告2.docx(19页珍藏版)》请在冰豆网上搜索。
高级网站设计试验报告2
武汉纺织大学《高级网站设计》实验报告
班级:
管工1202姓名:
梁星团实验时间:
2014年6月17日指导教师:
陈冈
一、实验目的
1.了解数据库连接池。
2.掌握Spring应用。
3.掌握Hibernate应用。
二、实验内容
1.创建一个基于SpringMVC的Web应用项目。
2.实现项目的国际化处理。
3.实现用户的登录、注册处理。
4.编写一个小型留言板系统,利用Hibernate实现留言板处理。
三、操作步骤
User.java
packagecounts;
publicclassUser{
privateintid;
privateStringusername;
privateStringpassword;
publicUser(){}
publicUser(intid,Stringusername,Stringpassword){
super();
this.id=id;
this.username=username;
this.password=password;
}
publicintgetId(){
returnid;
}
publicvoidsetId(intid){
this.id=id;
}
publicStringgetUsername(){
returnusername;
}
publicvoidsetUsername(Stringusername){
this.username=username;
}
publicStringgetPassword(){
returnpassword;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
}
UserDao.java
packagecounts;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.util.ArrayList;
importjava.util.List;
importjavax.sql.DataSource;
importorg.springframework.jdbc.core.JdbcTemplate;
importorg.springframework.jdbc.core.PreparedStatementCreator;
publicclassUserDao{
privateDataSourcedataSource;
publicvoidsetDataSource(DataSourcedataSource){
this.dataSource=dataSource;
}
publicvoidinsert(finalUseruser){
JdbcTemplatejdbcTemplate=newJdbcTemplate(dataSource);
jdbcTemplate.update(newPreparedStatementCreator(){
@Override
publicPreparedStatementcreatePreparedStatement(Connectionconn)
throwsSQLException{
Stringsql="insertintouser(username,password)values(?
?
)";
PreparedStatementps=conn.prepareStatement(sql);
ps.setString(1,user.getUsername());
ps.setString(2,user.getPassword());
returnps;
}
});
}
publicListgetUsers(Stringusername,Stringpassword)
throwsClassNotFoundException{
Listlist=newArrayList();
try{
Class.forName("com.mysql.jdbc.Driver");
Stringsql="select*fromuserwhereusername=?
andpassword=?
";
Connectionconn=DriverManager.getConnection(
"jdbc:
mysql:
//localhost/db_wen","root","520");
PreparedStatementps=conn.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,password);
ResultSetrs=ps.executeQuery();
if(rs.next()){
Useruser=newUser(rs.getInt
(1),rs.getString
(2),
rs.getString(3));
list.add(user);
}
ps.close();
conn.close();
}catch(SQLExceptione){
e.printStackTrace();
}
returnlist;
}
}
控制层代码:
Login.java
packagecontrollers;
importjava.io.IOException;
importjava.util.List;
importjavax.servlet.RequestDispatcher;
importjavax.servlet.ServletException;
importjavax.servlet.annotation.WebServlet;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importcounts.User;
importcounts.UserDao;
@WebServlet("/login.do")
publicclassLoginextendsHttpServlet{
privatestaticfinallongserialVersionUID=1L;
publicLogin(){
}
protectedvoiddoGet(HttpServletRequestrequest,
HttpServletResponseresponse)throwsServletException,IOException{
doPost(request,response);
}
protectedvoiddoPost(HttpServletRequestrequest,
HttpServletResponseresponse)throwsServletException,IOException{
Stringusername=request.getParameter("username");
Stringpassword=request.getParameter("password");
UserDaouserDao=newUserDao();
Listusers=null;
try{
users=userDao.getUsers(username,password);
RequestDispatcherrd;
if(users.size()>0)
rd=request.getRequestDispatcher("WEB-INF/admin.jsp");
else{
rd=request.getRequestDispatcher("login.jsp");
}
request.setAttribute("users",users);
rd.forward(request,response);
}catch(Exceptione){
e.printStackTrace();
}
}
}
Register.java
packagecontrollers;
importjava.io.IOException;
importjavax.servlet.ServletException;
importjavax.servlet.annotation.WebServlet;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importcounts.User;
importcounts.UserDao;
@WebServlet("/register.do")
publicclassRegisterextendsHttpServlet{
privatestaticfinallongserialVersionUID=1L;
publicRegister(){
}
protectedvoiddoGet(HttpServletRequestrequest,
HttpServletResponseresponse)throwsServletException,IOException{
doPost(request,response);
}
protectedvoiddoPost(HttpServletRequestrequest,
HttpServletResponseresponse)throwsServletException,IOException{
request.setCharacterEncoding("utf-8");
ApplicationContextcontext=newClassPathXmlApplicationContext(
"beans.xml");
UserDaoregister=(UserDao)context.getBean("userDao");
Useruser=newUser();
user.setUsername(request.getParameter("username"));
System.out.print(user.getUsername());
user.setPassword(request.getParameter("password"));
register.insert(user);
response.setContentType("text/html;charset=utf-8");
response.getWriter().println("恭喜你,注册成功!
");
}
}
视图层代码:
Login.jsp
<%@pagelanguage="java"contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglibprefix="c"uri="
Inserttitlehere
.login_table{
border:
mediumgroove#009999;
}
| | |
| | |
用户名
| |
密码
|
iftest="${users!
=null&&emptyusers}">
#FF0000">登录失败!
if>
Register.jsp
<%@pagelanguage="java"contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglibprefix="c"uri="
Inserttitlehere
.login_table{
border:
mediumgroove#009999;
}
| | |
| | |
用户名
| |
密码
|
iftest="${users!
=null&&emptyusers}">
#FF0000">注册失败!
if>
Admin.jsp
<%@pagelanguage="java"contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http:
//www.w3.org/TR/html4/loose.dtd">
Inserttitlehere
.login_table{
border:
mediumgroove#006699;
}
.STYLE1{
color:
#FF0000;
font-weight:
bold;
}
登录成功!