slowalker

导航

servlet session管理的四种方式 --隐藏表单

---恢复内容开始---

利用隐藏表单来保持状态,与采用网址重写技术类似,但是通过隐藏表单表单的方式,其url后不显式跟随值,而是将他们放到表单中,在提交表单时,其值传递至服务器。

使用这种方式时,需要页面中本身含有表单或自己构建一个表单来使用。可以传递更多的字符且不需要进行字符编码。

 

下文中通过隐藏表单的方式修改类的值。

package com.slowalker.rewrite;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HiddenForm
 */
@WebServlet(name="/HiddenForm", 
            urlPatterns= {"/customer", "/editCustomer", "/updateCustomer"}) 
           //一个servlet页面映射多个路径

public class HiddenForm extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ArrayList<Customer> customers = new ArrayList<>(2);
	
	public void init() 
			throws javax.servlet.ServletException {
		customers.add(new Customer(1 , "lp", "yk"));
		customers.add(new Customer(2, "hx", "xa"));
	}
    
    public HiddenForm() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String url = request.getRequestURI();
		if (url.endsWith("/customer")) sendCustomerList(request, response);
		if (url.endsWith("/editCustomer")) sendEditCustomerForm(request, response);
		
	}
	
	protected void sendCustomerList(HttpServletRequest request, HttpServletResponse response) 
				throws IOException, ServletException {
		response.setContentType("text/html");
		PrintWriter pw = response.getWriter();
		pw.println("<html><head><title>Customer List</title></head>"
				 + "<body>"
				 + "<table>");
		for (Customer c:customers) {
			pw.println("<tr><td>" + c.getId() +"</td><td>" + c.getName() + "</td><td>" + c.getCity() +"</td>"
					+ "<td>" + "<a href='editCustomer?id=" + c.getId() +"'>edit</a>" + "</td>" +"</tr>");
		}
		pw.println("</table></body></html>");
	}
	
	private Customer getCustomer(int id) {
		for (Customer c:customers) {
			if (c.getId() == id) return c;
		}
		return null;
		
	}
	
	protected void sendEditCustomerForm(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		response.setContentType("text/html");
		int customerId = 0;
		try {
			customerId = Integer.parseInt(request.getParameter("id"));
		}catch (NumberFormatException e){}
		Customer customer = getCustomer(customerId);
		PrintWriter pw = response.getWriter();
		if (customer != null) {
			pw.println("<html><head><title>edit customer</title></head>"
					 + "<body><h2>edit customer</h2>"
					 + "<form method='post' action='updateCustomer'>");
			/*
			 * 隐藏表单 
			 */
			pw.println("<input type='hidden' name='id' value='" + customer.getId() + "'/>");
			pw.println("<table>"
					 + "<tr><td>Name:</td><td>" + "<input name='name' value='" + customer.getName().replaceAll("'", "'") + "'/>" + "</td></tr>"
					 + "<tr><td>City:</td><td>" + "<input name='city' value='" + customer.getCity().replaceAll("'", "'") + "'/>" + "</td></tr>");
			pw.println("<tr><td conspan='2' style='text-align:right'>"
					 + "<input type='submit' value='Update'/></td></tr>");
			pw.println("<tr><td colspan='2'><a href='customer'>Customer List</a></td></tr>");
			pw.println("</table></body></html>");
		}else {
			pw.println("No Customer Found");
		}
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		int customerId = 0;
		try {
			customerId = Integer.parseInt(request.getParameter("id"));
		}catch (NumberFormatException e) {}
		
		Customer customer = getCustomer(customerId);
		if(customer != null) {
			customer.setName(request.getParameter("name"));
			customer.setCity(request.getParameter("city"));
		}
		sendCustomerList(request, response);
	}

}

 

posted on 2017-11-22 09:33  slowalker-lee  阅读(569)  评论(0)    收藏  举报