ตามหัวข้อเลยครับ คือ ผมกำลังฝึกเขียน JSP โดยความเข้าใจพื้นฐานคือ get จะแสดงตัวแปรให้เห็นว่าเราส่งค่าอะไรไป และ post จะซ่อนตัวแปร และจะทำงานแบบซับซ้อนได้ดีกว่า ซึ่งโค้ดที่ผมเขียนนี้ มันเป็นแบบฟอร์มที่ทำง่ายๆครับ (ผมเขียนตามใน youtube) แต่มีข้อสงสัยว่า เราส่งข้อมูลแบบ post ใน servlet จะรับข้อมูลมาใน dopost ใช่มั๊ยครับ แต่กรณีนี้ คือ ผมลองเอาข้อมูลใน doget ออก มันไม่สามารถทำงานได้ เลยอยากให้ช่วยแนะกระทู้ละเอียดเกี่ยวกับข้อนี้ด้วยครับ
อันนี้โค้ดที่ผมเขียนตามนะ
ส่วน Controller
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action =request.getParameter("actions");
HttpSession session = request.getSession();
if(action==null){
Account account = checkCookie(request);
if(account==null){
request.getRequestDispatcher("login.jsp").forward(request, response);
}else{
Accountmodel accmodel = new Accountmodel();
if(accmodel.login(account.getUsername(),account.getPassword()))
{
session.setAttribute("username",account.getUsername());
request.getRequestDispatcher("welcome.jsp").forward(request, response);
}else{
request.setAttribute("error","Account's Invalid");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
}
else{
if(action.equalsIgnoreCase("logout")){
session.removeAttribute("username");
Cookie[] cookies =request.getCookies();
for(Cookie ck:cookies){
if(ck.getName().equalsIgnoreCase("username")){
ck.setMaxAge(0);
response.addCookie(ck);
}
}
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}}
private Account checkCookie(HttpServletRequest request){
Cookie[] cookie = request.getCookies();
Account account = null;
if(cookie==null){
return null;}
else{
String username = "",password="";
for(Cookie ck:cookie){
if(ck.getName().equalsIgnoreCase("username"))
username=ck.getValue();
if(ck.getName().equalsIgnoreCase("password"))
password=ck.getValue();
}
if(!username.isEmpty()&&!password.isEmpty()){
account= new Account(username,password);
}
}
return account;
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
HttpSession session =request.getSession();
Accountmodel accModel = new Accountmodel();
if(action==null){
String username=request.getParameter("username").trim();
String password=request.getParameter("password").trim();
boolean remember =request.getParameter("remember")!=null;
if(accModel.login(username, password)){
session.setAttribute("username", username);
if(remember){
Cookie ckUsername= new Cookie("username",username);
ckUsername.setMaxAge(3600);
response.addCookie(ckUsername);
Cookie ckPassword= new Cookie("password",password);
ckPassword.setMaxAge(3600);
response.addCookie(ckPassword);
}
request.getRequestDispatcher("welcome.jsp").forward(request, response);
}
}else{
request.setAttribute("error","Account's Invalis");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
}
อยากรู้ถึงความแตกต่างของ GET กับ POSTครับ
อันนี้โค้ดที่ผมเขียนตามนะ
ส่วน Controller
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action =request.getParameter("actions");
HttpSession session = request.getSession();
if(action==null){
Account account = checkCookie(request);
if(account==null){
request.getRequestDispatcher("login.jsp").forward(request, response);
}else{
Accountmodel accmodel = new Accountmodel();
if(accmodel.login(account.getUsername(),account.getPassword()))
{
session.setAttribute("username",account.getUsername());
request.getRequestDispatcher("welcome.jsp").forward(request, response);
}else{
request.setAttribute("error","Account's Invalid");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
}
else{
if(action.equalsIgnoreCase("logout")){
session.removeAttribute("username");
Cookie[] cookies =request.getCookies();
for(Cookie ck:cookies){
if(ck.getName().equalsIgnoreCase("username")){
ck.setMaxAge(0);
response.addCookie(ck);
}
}
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}}
private Account checkCookie(HttpServletRequest request){
Cookie[] cookie = request.getCookies();
Account account = null;
if(cookie==null){
return null;}
else{
String username = "",password="";
for(Cookie ck:cookie){
if(ck.getName().equalsIgnoreCase("username"))
username=ck.getValue();
if(ck.getName().equalsIgnoreCase("password"))
password=ck.getValue();
}
if(!username.isEmpty()&&!password.isEmpty()){
account= new Account(username,password);
}
}
return account;
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
HttpSession session =request.getSession();
Accountmodel accModel = new Accountmodel();
if(action==null){
String username=request.getParameter("username").trim();
String password=request.getParameter("password").trim();
boolean remember =request.getParameter("remember")!=null;
if(accModel.login(username, password)){
session.setAttribute("username", username);
if(remember){
Cookie ckUsername= new Cookie("username",username);
ckUsername.setMaxAge(3600);
response.addCookie(ckUsername);
Cookie ckPassword= new Cookie("password",password);
ckPassword.setMaxAge(3600);
response.addCookie(ckPassword);
}
request.getRequestDispatcher("welcome.jsp").forward(request, response);
}
}else{
request.setAttribute("error","Account's Invalis");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
}