- Back to Home »
- bahan kuliah , java »
- Include dan Forward 02_Session Tracking dan Manajemen
Monday 3 June 2013
Tujuan
section ini adalah menunjukkan bagaimana cara menyimpan session user dan
mengakses informasi session itu pada halaman lain.
Percobaan ini akan menggunakan atau modifikasi hasil
latihan pada percobaan sebelumnya (Percobaan 1).
Langkah-langkahnya
adalah sebagai berikut :
·
Buka class LoginFormServlet,
lakukan modifikasi untuk menangani form ditampilkan untuk user yang belum
login, sehingga kodenya seperti berikut:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class
LoginFormServlet extends HttpServlet {
protected void
processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException,
IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out =
response.getWriter();
RequestDispatcher rd = null;
HttpSession session =
request.getSession();
if
(session.getAttribute("user") != null){
rd =
request.getRequestDispatcher("/success");
rd.forward(request,
response);
}
out.println("<html>");
out.println("<head>");
out.println("<title>Login
Form Page</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Riza
Falafi</h1>");
out.println("<form
action='login' method='POST'>");
out.println("<table
border='0'>");
out.println("<tr>");
out.println("<td>Username</td>");
out.println("<td>:
<input type='text' name='username' value='' /></td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Password</td>");
out.println("<td>:
<input type='password' name='password' value='' /></td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td><input
type='reset' name='reset' value='Reset' /></td>");
out.println("<td><input
type='submit' name='login' value='Login' /></td>");
out.println("</tr>");
out.println("</table>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
out.close();
}
protected void
doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,
IOException {
processRequest(request,
response);
}
protected void
doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,
IOException {
processRequest(request,
response);
}
}
·
Buka web.xml pastikan saat mapping
servlet menggunakan nama yang sama dengan action form.
·
Pastikan servlet mapping-nya
seperti berikut :
<servlet-name>LoginFormServlet</servlet-name>
<servlet-class>jeni3.servlet.LoginFormServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>jeni3.servlet.LoginServlet</servlet-class>
</servlet>
<servlet> import java.io.*;
·
Sama seperti langkah diatas, buat buat
class Servlet caranya adalah klik kanan packages jeni3.servlet
pilih servlet isi nama ( contoh LoginServlet ) isikan kodenya.
Contoh
kodenya :
import
javax.servlet.*;
import
javax.servlet.http.*;
public class
LoginServlet extends HttpServlet {
protected void
doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
//initializing...
RequestDispatcher
rd = null;
HttpSession
session = request.getSession();
if
(session.getAttribute("user") != null){
rd =
request.getRequestDispatcher("/success");
rd.forward(request,
response);
}
String userName =
request.getParameter("username");
String password =
request.getParameter("password");
if
(authenticate(userName, password)){
session.setAttribute("user",userName);
rd =
request.getRequestDispatcher("/success");
rd.forward(request,
response);
}else{
//failed handler
response.setContentType("text/html;charset=UTF-8");
PrintWriter out =
response.getWriter();
out.println("User
does not exist with given username and/or password");
//return to login
page
rd =
request.getRequestDispatcher("/login.html");
rd.include(request,
response);
out.close();
}
}
public boolean
authenticate(String username, String password){
//use hardcode
username and password. (riza falafi and falafi)
return
(username.equals("riza falafi") &&
password.equals("rizafalafi"));
}
·
Lalu
modifikasikan class SuccessServlet untuk menampilkan informasi user, kodenya
adalah seperti berikut :
import java.io.*;
import
javax.servlet.*;
import
javax.servlet.http.*;
public class
SuccessServlet extends HttpServlet {
protected void
processRequest(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException { HttpSession session = request.getSession();
RequestDispatcher
rd = null;
response.setContentType("text/html;charset=UTF-8");
PrintWriter out =
response.getWriter();
if
(session.getAttribute("user") == null){
out.println("<font
color='red'>You are not log in. Please login here</font>");
rd =
request.getRequestDispatcher("/login.html");
rd.include(request,
response);
}else{
rd =
request.getRequestDispatcher("temp/header.html");
rd.include(request,
response);
rd =
request.getRequestDispatcher("temp/mainContent.html");
rd.include(request,
response);
out.println("<h2>Hello
<font
color='red'>"+session.getAttribute("user")+"</font>
<a href='./logout'>Logout</a></h2>");
rd =
request.getRequestDispatcher("temp/footer.html");
rd.include(request,
response);
}
out.close();
}
protected void
doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
processRequest(request,
response);
}
protected void
doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
processRequest(request,
response);
}
}
·
Kemudian buat class Servlet baru
Caranya
adalah klik kanan packages jeni3.servlet pilih
servlet isi nama ( contoh LogoutServlet
) isikan kodenya.
Contoh
kodenya :
package
jeni3.servlet;
import java.io.*;
import
javax.servlet.*;
import
javax.servlet.http.*;
public class
LogoutServlet extends HttpServlet {
protected void
processRequest(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out =
response.getWriter();
RequestDispatcher
rd =null;
HttpSession
session = request.getSession();
if
(session.getAttribute("user") != null){
session.removeAttribute("user");
session.invalidate();
out.println("You
have logged out successfully");
}
rd =
request.getRequestDispatcher("/loginForm");
rd.forward(request,
response);
out.close();
}
protected void
doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
processRequest(request,
response);
protected void
doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
processRequest(request,
response);
}
}
·
Pastikan servlet mapping-nya seperti berikut :
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>jeni3.servlet.LogoutServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>AuthenticationFilter</servlet-name>
<servlet-class>jeni3.servlet.filter.AuthenticationFilter</servlet-class>
</servlet>
<servlet-mapping>
·
Kemudian klik Run Project