`

servlet cookie jsp

阅读更多
首先是登陆界面,其中有mes_zh等国际化 这个不重要
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"  %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<style>
body{
fontsize:12px;
}
#keyword,#iskey{
color:red;
}
</style>
<html>
  <head>
  </head>
  <body>
<%-- ${cookie}--%>
    <fmt:setBundle basename="mes"/>
    <span style="color:red;fontSize:15px">${LogFail}</span>
    <form action="login.do" method="post">
    <fmt:message key="username"></fmt:message><input type="text" name="username"/>
    <fmt:message key="password"></fmt:message></fmt><input type="password" name="password"/>
    <input style="margin-left: 50px"type="checkbox" name="remeber" value="yes"/>
    <span style="color:red">是否记住密码?</span><br/>
    <input style="margin-top:20px" type="submit" value="<fmt:message key="login"></fmt:message>"/>
    </form>
    <table>
    <tr><th colspan="6"><fmt:message key="key"></fmt:message><span id="keyword">${keyWord}</span></th></tr>
  <tr><th><fmt:message key="id"></fmt:message></th><th><fmt:message key="name"></fmt:message></th>
  <th><fmt:message key="address"></fmt:message></th><th><fmt:message key="phone"></fmt:message></th>
  <th><fmt:message key="email"></fmt:message></th><th><fmt:message key="sex"></fmt:message></th></tr>
  <c:forEach items="${contacts}" var="con">
  <tr><td>${con.id}</td><td id="iskey">${con.name}</td><td>${con.address}</td><td>${con.phone }</td><td>${con.email}</td><td>${con.sex }</th></tr>
  </c:forEach>
  </table>
  </body>
</html>

接下来是上面post请求的servlet类
package com.ourchr.addressbook.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bo.UserBO;
import entity.User;

public class LoginServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
Cookie cookie = null;
String username = null;
String password = null;
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {

cookie = cookies[i];
System.out.println(cookie.getName()+"++++"+cookie.getValue());
if (cookie.getName().equals("username")){
username = cookie.getValue();

}
if(cookie.getName().equals("password")){
password = cookie.getValue();

}
}
}
UserBO userBO =new UserBO();
User user = userBO.login(username, password);
if(user==null){
request.getRequestDispatcher("/login.jsp").forward(request, response);
}else{
request.getSession().setAttribute("user", user);
String url = (String) request.getAttribute("requestURL");
response.sendRedirect(url);
return;
}
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html");
String username = request.getParameter("username");
String password = request.getParameter("password");
String remeber = request.getParameter("remeber");
System.out.println(username+"==="+password+"==="+remeber);
UserBO userBO = new UserBO();
User user = userBO.login(username, password);
if (user == null) {
request.setAttribute("LogFail", "用户名或密码错误,请重新登录");
request.getRequestDispatcher("/login.jsp").forward(request,
response);
} else {
request.getSession().setAttribute("user", user);
if (remeber.equals("yes")) {
Cookie cookie = new Cookie("username", username);
cookie.setMaxAge(3600 * 24 * 30);
response.addCookie(cookie);
cookie= new Cookie("password", password);
cookie.setMaxAge(3600 * 24 * 30);
response.addCookie(cookie);
request.getSession().setAttribute("user", user);
System.out.println(request.getCookies().length+"------");
}
request.getRequestDispatcher("/choseItem.jsp").forward(request,
response);
}
}

}

剩下的都是容易实现的了
我把该登陆用户的功能分为两部分
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="h" tagdir="/WEB-INF/tags" %>

<style>
body{
fontsize;12px;
}
#link{
width:100px;
height:50px;
}
</style>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
 
  <body>
  <h:ckeckLogin></h:ckeckLogin>
  <div style="color:blue;fontsize;15px;margin-bottom:50px">${LogSuccess}</div>
<div id="link">
  <a href="addContact.jsp" id="addLink">增加联系人</a>
  </div>
  <div id="link">
  <a href="findContact.jsp" id="findLink">查找联系人</a>
  </div>
 
  </body>
</html>
那接下来增加和查找也顺便写下吧
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP 'checkLogin.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
 
  <body>
  <hr/>
  添加失败的用户信息: <c:out value="${Conexception}"></c:out>
  <hr/>
   <form action="doAddContact.jsp" method="post">
   <table>
   <tr>
   <td>姓名:</td><td><input type="text" name="username"/></td>
   </tr>
   <tr>
   <td>手机:</td><td><input type="text" name="phone"/></td>
   </tr>
   <tr>
   <td>Email:</td><td><input type="text" name="email"/></td>
   </tr>
   <tr>
   <td>地址:</td><td><input type="text" name="address"/></td>
   </tr>
   <tr>
   <td>性别:</td>
   <td>
   男<input type="radio" name="sex" value="male" ckecked="checked" >
   女<input type="radio" name="sex" value="female" >
   </td>
   </tr>
   <tr>
   <td colspan="2">
   <input type="submit" value="提交"/><input type="reset" value="重置"/>
   </td>
   </tr>
   </table>
   </form>
  </body>
</html>
<%@ page language="java" import="java.util.*,com.ourchr.assdressbook.entity.*,entity.*,com.ourchr.addressbook.bo.*,com.ourchr.addressbook.exception.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'doAddContact.jsp' starting page</title>
   
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">   
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

  </head>
  <body>
 
    <%
    request.setCharacterEncoding("utf-8");
    String name =request.getParameter("username");
    String phone =request.getParameter("phone");
    String email = request.getParameter("email");
    String address = request.getParameter("address");
    String sex = request.getParameter("sex");
    Contact contact = new Contact();
    contact.setName(name);
    contact.setPhone(phone);
    contact.setEmail(email);
    contact.setAddress(address);
    contact.setSex(sex);
  contact.setOwner((User)session.getAttribute("user"));
  session.setAttribute("contact",contact);
    ContactBO contactBO = new ContactBO();
    %>
    <c:catch var="ContactExistException">
    <%
    contactBO.addContact(contact);
    %>
    </c:catch>
    <c:if test="${not empty ContactExistException}">
     <c:set var="Conexception" value="${ContactExistException.message}" scope="request"></c:set>
    <jsp:forward page="/addContact.jsp"></jsp:forward>
    </c:if>
    <c:if test="${empty ContactExistException}">
    <jsp:forward page="/showContact.jsp"></jsp:forward>
    </c:if>
 
  </body>
</html>


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>
  <h1>给用户提供查询联系人:</h1>
  <form action="doFindContact.jsp" method="post">
  <input type="text" name="conName"/>
  <input type="submit">
  </form>
  </body>
</html>


<%@ page language="java" import="java.util.*,entity.*,com.ourchr.assdressbook.entity.*,com.ourchr.addressbook.bo.ContactBO" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>
  <%
   request.setCharacterEncoding("utf-8");
    String conNameIndex =request.getParameter("conName");
    User owner= (User)session.getAttribute("user");
    ContactBO contactBO = new ContactBO();
    List<Contact> contacts=contactBO.findContacts(owner,conNameIndex);
    request.setAttribute("keyWord",conNameIndex);
    request.setAttribute("contacts",contacts);
    %>
    <jsp:forward page="login.jsp"></jsp:forward>
  </body>
</html>


那几本上都差不多了吧
还有一个标签
<%@ tag pageEncoding="utf-8" body-content="scriptless" import="entity.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
User user = (User)session.getAttribute("user");
if(user == null){
String url = request.getRequestURL().toString();
request.setAttribute("requestURL",url);
request.getRequestDispatcher("login.do").forward(request,response);
}
%>

完成!


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics