package hidden;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* 隐藏域:(简单来说:服务器发送HTML文档,将信息放在其标签中用来表示特定用户,并进行隐藏)
* 隐藏域有点类似于URL重写技术,但不是将至附加到URL上,而是放到HTML的隐藏域中。
* 优点:相对URL重写技术没有字符限制,而且无需额外编码。
* 缺点:同URL一样,不适合跨越多个界面
*/
@WebServlet(name="hiddenCope",urlPatterns= {"/hidden","/customer","/customerEdit","/updateCustomer"})
public class hiddenCope extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
//建立两个用户
private List<customer> customers;
private customer customer1;
private customer customer2;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取uri
String uri=req.getRequestURI();
//根据请求的不同发送不同的页面
if(uri.endsWith("/customer")) { //请求中包含customer
sendCustomerInfo(req,resp);
}else if(uri.endsWith("/customerEdit")){
sendEditform(req,resp);
}
}
//发送客户表单
private void sendEditform(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//获取请求客户的id
String id=req.getParameter("id");//记录客户端客户
System.out.println(id);
PrintWriter pw=resp.getWriter(); //获取响应打印对象
//向客户端发送数据
pw.println("<html><head></head><body>");
//body部分
pw.println("<form method='post'>");
//建一张表
pw.println("<table>");
//建立id号输入框
pw.println("id:<input name='id' type='hidden' id='id' value="+id+">" ); //设置隐藏域
//建立姓名输入框
pw.println("name:<input type='text' name='name'>");
//建立住址输入框
pw.println("city:<input type='text' name='city'>");
pw.println("</table>");
//添加提交按钮
pw.println("<input type='submit'/>");
pw.println("</form>");
pw.println("</body></html>");
}
/*
* 发送客户信息
* 第一次请求通过URL重写进行状态保存
*/
private void sendCustomerInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter pw=resp.getWriter();
//输出用户信息
pw.println("<html><head></head><body>"
+ "customers infomation:");
for (customer user :customers) {
pw.println(
"<br/>"
+"userId:"
+user.getId() //输出id号
+"</br>"
+user.getName() //输出姓名
+"</br>"
+user.getCity()+user.getId() //输出城市
+"<a href='customerEdit?id="+"'>edit</a>"); //连接到editcustomer传回id
}
pw.println("</body></html>");
}
//接受客户端发来的请求,并改变客户信息,并返回改变后的值
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//得到请求中客户的信息
String id=req.getParameter("id");
System.out.println(id);
String name=req.getParameter("name");
System.out.println(name);
String city=req.getParameter("city");
System.out.println(city);
//设置客户信息
for (customer user : customers) {
if(id.equals(user.getId())) {
user.setName(name);
user.setCity(city);
}
}
//输出改变后的页面
sendCustomerInfo(req, resp);
}
@Override
public void init() throws ServletException {
//实例化两位用户
customer1=new customer();
customer2=new customer();
//第一位客户
customer1.setId("04151125");
customer1.setName("david");
customer1.setCity("london");
//第二位客户
customer2.setId("04151126");
customer2.setName("qiaobusi");
customer2.setCity("paris");
//创建列表
customers=new ArrayList<customer>();
customers.add(customer1);
customers.add(customer2);
}
}
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* 隐藏域:(简单来说:服务器发送HTML文档,将信息放在其标签中用来表示特定用户,并进行隐藏)
* 隐藏域有点类似于URL重写技术,但不是将至附加到URL上,而是放到HTML的隐藏域中。
* 优点:相对URL重写技术没有字符限制,而且无需额外编码。
* 缺点:同URL一样,不适合跨越多个界面
*/
@WebServlet(name="hiddenCope",urlPatterns= {"/hidden","/customer","/customerEdit","/updateCustomer"})
public class hiddenCope extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
//建立两个用户
private List<customer> customers;
private customer customer1;
private customer customer2;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取uri
String uri=req.getRequestURI();
//根据请求的不同发送不同的页面
if(uri.endsWith("/customer")) { //请求中包含customer
sendCustomerInfo(req,resp);
}else if(uri.endsWith("/customerEdit")){
sendEditform(req,resp);
}
}
//发送客户表单
private void sendEditform(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//获取请求客户的id
String id=req.getParameter("id");//记录客户端客户
System.out.println(id);
PrintWriter pw=resp.getWriter(); //获取响应打印对象
//向客户端发送数据
pw.println("<html><head></head><body>");
//body部分
pw.println("<form method='post'>");
//建一张表
pw.println("<table>");
//建立id号输入框
pw.println("id:<input name='id' type='hidden' id='id' value="+id+">" ); //设置隐藏域
//建立姓名输入框
pw.println("name:<input type='text' name='name'>");
//建立住址输入框
pw.println("city:<input type='text' name='city'>");
pw.println("</table>");
//添加提交按钮
pw.println("<input type='submit'/>");
pw.println("</form>");
pw.println("</body></html>");
}
/*
* 发送客户信息
* 第一次请求通过URL重写进行状态保存
*/
private void sendCustomerInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter pw=resp.getWriter();
//输出用户信息
pw.println("<html><head></head><body>"
+ "customers infomation:");
for (customer user :customers) {
pw.println(
"<br/>"
+"userId:"
+user.getId() //输出id号
+"</br>"
+user.getName() //输出姓名
+"</br>"
+user.getCity()+user.getId() //输出城市
+"<a href='customerEdit?id="+"'>edit</a>"); //连接到editcustomer传回id
}
pw.println("</body></html>");
}
//接受客户端发来的请求,并改变客户信息,并返回改变后的值
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//得到请求中客户的信息
String id=req.getParameter("id");
System.out.println(id);
String name=req.getParameter("name");
System.out.println(name);
String city=req.getParameter("city");
System.out.println(city);
//设置客户信息
for (customer user : customers) {
if(id.equals(user.getId())) {
user.setName(name);
user.setCity(city);
}
}
//输出改变后的页面
sendCustomerInfo(req, resp);
}
@Override
public void init() throws ServletException {
//实例化两位用户
customer1=new customer();
customer2=new customer();
//第一位客户
customer1.setId("04151125");
customer1.setName("david");
customer1.setCity("london");
//第二位客户
customer2.setId("04151126");
customer2.setName("qiaobusi");
customer2.setCity("paris");
//创建列表
customers=new ArrayList<customer>();
customers.add(customer1);
customers.add(customer2);
}
}