java Web session Technology (cookie)
Keywords:
Java
encoding
Session
xml
java Web(cookie) session Technology
Set xml configuration information
<!-- ************************** -->
Create a select -- >
< servlet name > demo05 < / servlet name > <! -- 2. Name select -- >
< servlet class > come. Lanou3g. Demo05 < / servlet class > <! -- 3. Set select address -- >
</servlet>
<!-- ************************** -->
< servlet mapping > <! -- register configuration access path -- >
< servlet name > demo05 < / servlet name > <! -- configure the name of this component to be consistent with the previous -- >
< URL pattern > / servlet / demo05 < / url pattern > <! -- configure the access address of this component -- >
</servlet-mapping>
<!-- ************************** -->
<!-- ************************** -->
Create a select -- >
< servlet name > demo06 < / servlet name > <! -- 2. Name select -- >
< servlet class > come. Lanou3g. Demo06 < / servlet class > <! -- 3. Set select address -- >
</servlet>
<!-- ************************** -->
< servlet mapping > <! -- register configuration access path -- >
< servlet name > demo06 < / servlet name > <! -- configure the name of this component to be consistent with the previous -- >
< URL pattern > / servlet / demo06 < / url pattern > <! -- configure the access address of this component -- >
</servlet-mapping>
<!-- ************************** -->
<!-- ************************** -->
Create a select -- >
< servlet name > demo07 < / servlet name > <! -- 2. Name select -- >
< servlet class > come. Lanou3g. Demo07 < / servlet class > <! -- 3. Set select address -- >
</servlet>
<!-- ************************** -->
< servlet mapping > <! -- register configuration access path -- >
< servlet name > demo07 < / servlet name > <! -- configure the name of this component to be consistent with the previous -- >
< URL pattern > / servlet / demo07 < / url pattern > <! -- configure the access address of this component -- >
</servlet-mapping>
1. Set last access time
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Demo05 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("lastTime")) {
String value = cookie.getValue();
long time = Long.parseLong(value);
Date date = new Date(time);
SimpleDateFormat dateFormat =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String lastTime = dateFormat.format(date);
response.getWriter().write("Last visit time:" + lastTime);
}
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
2. Set up the form and print the last access time
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Demo06 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
String username = "";
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("username")) {
username = cookie.getValue();
}
}
}
System.out.println(username);
response.getWriter().write("<form action='demo07' method='post'>");
response.getWriter().write("User name:<input type=\"text\" name=\"username\"><br/>");
response.getWriter().write("Password:<input type=\"text\" name=\"password\"><br/>");
response.getWriter().write("remember:<input type=\"checkbox\" name=\"remember\" value=\"\"><br/>");
response.getWriter().write("<input type=\"submit\" value= \"Sign in\">");
response.getWriter().write("</form>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
3. Accept form data and record user cookie s
package come.lanou3g;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Demo07 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String remember = request.getParameter("remember");
Cookie cookie = new Cookie("username", username);
cookie.setPath("/sh-web-servlet003/servlet");
if(username.equals("wangl")
&& password.equals("123")){
if (remember != null) {
System.out.println(remember + "123123123");
cookie.setMaxAge(Integer.MAX_VALUE);
}else {
cookie.setMaxAge(0);
}
response.addCookie(cookie);
response.getWriter().write("Login successfully");
}else {
response.getWriter().write("Login failed");
response.setHeader("refresh", "3;url=demo06");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
Posted by seodevhead on Sat, 04 Apr 2020 11:11:53 -0700