[java]代码库
package chapter03.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
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 chapter03.model.User;
/**
* Servlet implementation class loginServlet
*/
public class loginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public loginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=GBK");
String action = request.getParameter("action");
if ("login".equals(action)){
login(request, response);
}
else if ("check".equals(action)){
check(request, response);
}
else if ("logout".equals(action)){
logout(request, response);
}
else{
unlogin(request, response);
}
}
private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String loginUrl = "/login.html";
String fileUrl = "/login?action=check";
HttpSession session = request.getSession();
String userName = request.getParameter("user_name");
String password = request.getParameter("login_password");
// 验证用户ID和密码是否合法,略...
ServletContext context = getServletContext();
if (userName == null || userName.equals("")) {
RequestDispatcher dispatcher =
context.getRequestDispatcher(loginUrl);
response.setContentType("text/html;charset=GBK");
dispatcher.forward(request, response);
return;
}
// 登录成功,设置session中的属性
else{
User user = new User();
user.setUserName(userName);
session.setAttribute("User", user);
RequestDispatcher dispatcher = context.getRequestDispatcher(fileUrl);
dispatcher.forward(request, response);
}
}
private void check(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
User user = (User)session.getAttribute("User");
if (user == null) { // 若未登陆,则先跳转到登录页面
response.setContentType("text/html;charset=GBK");
response.setHeader("Refresh", "3; URL=login.html");
PrintWriter out = response.getWriter();
out.println("<FONT color='red'>请先登录!</FONT><BR>");
out.println("<FONT color='blue'>3秒后跳转到登录页面...</FONT>");
out.close();
return;
}
else if(user.getUserName().equals("sss"))
{
String loginURL = "login?action=login";
String logoutURL = "login?action=logout";
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
out.println("<HTML><HEAD><TITLE>MailServlet:check</TITLE></HEAD>");
out.println("<body>");
out.println("当前用户为:" + user.getUserName());
out.println("<p><p>");
out.println("你的邮箱中有100封邮件");
out.println("<p><p>");
out.println("<a href='" + loginURL + "'>登陆</a>");
out.println("<a href='" + logoutURL + "'>注销</a>");
out.println("</body></html>");
out.close();
}
else
{
String loginURL = "login?action=login";
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
out.println("<HTML><HEAD><TITLE>MailServlet:check</TITLE></HEAD>");
out.println("<body>");
out.println(user.getUserName() + "您好!");
out.println("<p><p>");
out.println("<FONT color='red'>您无权访问邮件系统。</FONT>");
out.println("<p><p>");
out.println("请重新<a href='" + loginURL + "'>登陆</a>邮件系统!!");
out.println("</body></html>");
out.close();
}
}
private void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
User user = (User)session.getAttribute("User");
String name = user.getUserName();
session.invalidate();
String loginURL = "login?action=login";
PrintWriter out = response.getWriter();
out.println("<HTML><HEAD><TITLE>MailServlet:logout</TITLE></HEAD>");
out.println("<body>");
out.println(name + ",再见!");
out.println("<p><p>");
out.println("<a href='" + loginURL + "'>重新登录邮件系统</a>");
out.println("</body></html>");
out.close();
}
private void unlogin(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
String loginURL = "login?action=login";
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
out.println("<HTML><HEAD><TITLE>MailServlet:unlogout</TITLE></HEAD>");
out.println("<body>");
out.println("您好!");
out.println("<p><p>");
out.println("请您先<a href='" + loginURL + "'>登陆</a>邮件系统!");
out.println("</body></html>");
out.close();
}
}
初级程序员
by: 雪夜蓝风 发表于:2014-04-22 07:53:36 顶(0) | 踩(0) 回复
代码耦合度过高
回复评论