📄 loginservlet.java
字号:
package com.ata.shoping;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;
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 LoginServlet extends HttpServlet {
static Vector<Product> productList;
/**
* Constructor of the object.
*/
public LoginServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//...............验证用户
String loginName = "Liyang";
//...............建立并保存session
HttpSession session =request.getSession();
session.setAttribute("user", loginName);
//...............初始化购物车,并保村到session;
session.setAttribute("myCard", new Card());
//..............转向到商品页面,让客户浏览并购买产品
response.sendRedirect("/shopingcard/productList.jsp");
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
productList = new Vector<Product>();
Product p1 = new Product();
p1.setProductId("1");
p1.setProductName("PC");
p1.setProductPrice(4500);
p1.setProductImage("/image/p1.jpg");
Product p2 = new Product();
p2.setProductId("2");
p2.setProductName("PC2");
p2.setProductPrice(5500);
p2.setProductImage("/image/p2.jpg");
Product p3 = new Product();
p3.setProductId("3");
p3.setProductName("PC3");
p3.setProductPrice(74500);
p3.setProductImage("/image/p3.jpg");
productList.add(p1);
productList.add(p2);
productList.add(p3);
}
public static Vector<Product> getProductAll(){
return productList;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -