高级网站设计试验报告2.docx
- 文档编号:25110807
- 上传时间:2023-06-05
- 格式:DOCX
- 页数:19
- 大小:293.05KB
高级网站设计试验报告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;
}
});
}
publicList
throwsClassNotFoundException{
List
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();
List
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="
.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="
.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">
.login_table{
border:
mediumgroove#006699;
}
.STYLE1{
color:
#FF0000;
font-weight:
bold;
}
配置文件bean
Beans.xml
xmlversion="1.0"encoding="UTF-8"?
>
//www.springframework.org/schema/beans" xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation="http: //www.springframework.org/schema/beanshttp: //www.springframework.org/schema/beans/spring-beans.xsd"> mysql: //localhost/db_wen? useUnicode=true&characterEncoding=UTF-8"/>
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 高级 网站 设计 试验报告
![提示](https://static.bdocx.com/images/bang_tan.gif)