实验十
MVC
的实现
一、实验目的
通过实验掌握下列知识 :
1 、掌握 MVC 的基本概念
2 、掌握符合 MVC 架构的 web 应用的开发方法
二、实验内容及步骤
1 、 MVC 的实现
1 )按照下图设计并实现网页内容:
图( 1 )
a) 开发符合 MVC 架构的 web 应用以实现以上页面功能及流向;
b) 首页包含用户登录表单以及用户注册的链接
c)
用户登录时,使用 js 技术对用户填写的信息进行简单检查( <FORM onSubm
it=”return 调用函数名 ” >…</FORM> ),若检查出错需在首页上提示错误信息;若语法检查通过,则判断用户名是否存在数据库中等,若出现错误则显示报错页面( failed.jsp );若无错误发生则显示欢迎页面,显示 “ 欢迎您, ***” ;
d) (补充, 2-3 人小组完成)
注册页面包含新用户信息注册表单,用户提交后能使用 js 技术对用户填写的信息进行简单检查,若检查出错在注册页面上提示错误信息;若语法检查通过,则判断用户名是否已存在数据库中等,若出现错误则显示报错页面( failed.jsp );若无错误发生则将新用户记录在数据库中,并显示欢迎页面,显示 “ 欢迎您, ***” ;
注册页面包含新用户信息注册表单,用户提交后能使用 js 技术对用户填写的信息进行简单检查,若检查出错在注册页面上提示错误信息;若语法检查通过,则判断用户名是否已存在数据库中等,若出现错误则显示报错页面( failed.jsp );若无错误发生则将新用户记录在数据库中,并显示欢迎页面,显示 “ 欢迎您, ***” ;
不知为何,我用 MyEclipse 编写的 js 总是不能使用,所以第 c 题和第 d 题,我现在采用了是判断直接写到 servlet 中,用它来做相关判断
一.登录模块:
登录界面:
源代码:
<%@ page language=
"java" contentType=
"text/html; charset=GB2312"
pageEncoding=
"GB2312"%>
<! 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=GB2312">
< title > 用户登录 </ title >
</ head >
< body >
< form id=
"mainform" action=
"/TestMVC/servlet/CheckLogin" method=
"post">
用户名: < input type=
text name=
loginName size=
20><br>
密码: < input type=
password name=
password size=
20><br>
< input type=
submit value=
"
登录
" onclick="">
</ form >
< a href =
"Register.jsp"> 注册
</ a >
</ body >
</ html >
用于调度的 Servlet
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import user.User;
public
class
CheckLogin
extends HttpServlet {
/**
* Constructor of the object.
*/
public CheckLogin() {
super ();
}
/**
* Destruction of the
servlet . <br>
*/
public
void destroy() {
super .destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the
servlet . <br>
*
* This method is called when a form has its tag value method equals to get.
*
*
@param request the request send by the client to the server
*
@param response the response send by the server to the client
*
@throws ServletException if an error occurred
*
@throws IOException if an error occurred
*/
public
void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
String loginName = request.getParameter("loginName");
String password = request.getParameter("password");
response.setContentType("text/
html");
PrintWriter out = response.getWriter();
User user = new User(loginName,password);
if(user.login()){
out.
println("Login OK");
}
else{
out.
println("Failed to Login");
}
*/
PrintWriter out = response.getWriter();
out.println( "hgioahogia" );
}
/**
* The doPost method of the
servlet . <br>
*
* This method is called when a form has its tag value method equals to post.
*
*
@param request the request send by the client to the server
*
@param response the response send by the server to the client
*
@throws ServletException if an error occurred
*
@throws IOException if an error occurred
*/
public
void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String loginName = request.getParameter( "loginName" );
String password = request.getParameter( "password" );
// 加入数据为空 , 则直接返回登录界面
if (loginName==
null ||loginName.equals( "" )||password==
null ||password.equals( "" )){
response.sendRedirect( "/TestMVC/Login.jsp" );
}
else {
response.setContentType( "text/html" );
PrintWriter out = response.getWriter();
User user =
new User(loginName,password);
out.println(user.getLoginName());
if (ManageUser.
userLogin(user)){
out.println( "Login OK" );
HttpSession session = request.getSession();
session.setAttribute( "key" , user);
RequestDispatcher dispatcher = request.getRequestDispatcher( "/Welcome.jsp" );
dispatcher.forward(request, response);
}
else {
RequestDispatcher dispatcher = request.getRequestDispatcher( "/Failed.jsp" );
dispatcher.forward(request, response);
}
}
}
/**
* Initialization of the