Java Web development is a session based store implementation method
- 2020-04-01 04:17:18
- OfStack
This article illustrates a session-based store implementation for Java Web development. Share with you for your reference, as follows:
package cn.com.shopping;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//Complete the purchase
public class BuyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id=request.getParameter("id");
Book book=(Book)Db.getAll().get(id);
//Combined with the close when the session Cookie JiJue scheme
//Solution when blocking sessions
HttpSession session=request.getSession(false);
//Get the user's collection of all the books saved from session (shopping cart)
List list=(List)session.getAttribute("list");
if(list==null)
{
list=new ArrayList();
session.setAttribute("list", list);
}
list.add(book);
String url=response.encodeRedirectURL("/Session/SessionCountDemo");
response.sendRedirect(url);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
package cn.com.shopping;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//According to the book
public class ListBookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
HttpSession session=request.getSession();
out.print(" We have the following products: <br/>");
Map<String ,Book > map=Db.getAll();
for(Map.Entry<String, Book> entry:map.entrySet())
{
Book book=entry.getValue();
String url=response.encodeURL("/Session/BuyServlet?id="+book.getId());
out.print(book.getName()+"<a href='"+url+"' target='_blank' > buy </a><br/>");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
//Db as database
class Db
{
private static Map<String ,Book> map=new LinkedHashMap();
static
{
map.put("1", new Book("1","Java WEB The development of ","WY"," good book"));
map.put("2", new Book("2","WEB The development of ","zt"," general "));
map.put("3", new Book("3"," The program design ","df"," good book"));
map.put("4", new Book("4"," Computer composition ","as"," The general good book"));
map.put("5", new Book("5"," Compilation principle ","ty"," Very good book"));
map.put("6", new Book("6"," Network maintenance ","hj"," Very good book"));
}
public static Map getAll()
{
return map;
}
}
//book
class Book
{
private String id;
private String name;
private String author;
private String description;
public Book() {
super();
// TODO Auto-generated constructor stub
}
public Book(String id, String name, String author, String description) {
super();
this.id = id;
this.name = name;
this.author = author;
this.description = description;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
package cn.com.shopping;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class SessionCountDemo extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
HttpSession session=request.getSession();
if(session==null)
{
out.write(" You didn't buy anything! ");
return;
}
out.write(" You have purchased the following goods: ");
List<Book> list=(List) session.getAttribute("list");
for(Book book:list)
{
out.write(book.getName());
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
I hope this article has been helpful to your Java web programming.