Last active
August 29, 2015 14:13
-
-
Save dineshkp-dev/2f64e476c6dd2bd8a3d6 to your computer and use it in GitHub Desktop.
Servlet code for the login page.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.myshop.servlets; | |
import java.io.IOException; | |
import javax.servlet.RequestDispatcher; | |
import javax.servlet.ServletException; | |
import javax.servlet.annotation.WebServlet; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import javax.servlet.http.HttpSession; | |
//import com.shoppe.DAO.DAObject; | |
/** | |
* Servlet implementation class LoginServlet | |
*/ | |
@WebServlet(description = "Java Servlet that handles the User login", urlPatterns = { "/LoginServlet" }) // This code is autmatically aded by Eclipse | |
// Basically, it lets the program know how to call this Servlet | |
public class LoginServlet extends HttpServlet { | |
private static final long serialVersionUID = 1L; | |
// Create private fields to store username and password | |
private String uname = ""; | |
private String pwd = ""; | |
/** | |
* @see HttpServlet#HttpServlet() | |
*/ | |
public LoginServlet() { | |
super(); | |
// TODO Auto-generated constructor stub | |
} | |
/** | |
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) | |
*/ | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
// TODO Auto-generated method stub | |
} | |
/** | |
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) | |
*/ | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
// Call the doProcess method which handles the checking | |
doProcess(); | |
} | |
protected void doProcess (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
// Get the current session object from the 'HttpServletRequest' object 'request' that is available | |
HttpSession session = request.getSession(); | |
// Get the username and password | |
// Note: we should provide the same name (uname & pwd in this example) that the HTML (or JSP) passes | |
// Remember in the HTML page we have : | |
// <td align="middle"><input type="text" name="uname" size="32" value=""></td> | |
// <td align="middle"><input type="password" name="pwd" size="32" value=""></td> | |
uname = request.getParameter("uname"); | |
pwd = request.getParameter("pwd"); | |
// Add the username and password as attributes to the session object | |
session.setAttribute("uname", uname); | |
session.setAttribute("pwd", pwd); | |
// For now, we are hard-coding the authentication!!! Later it will be modified to a DAO object | |
if (uname.equals("user1" && pwd.equals("user1")) { | |
// If authentication succeeds, we want to proceed to the ShoppingArea | |
RequestDispatcher requestDispatcher = request.getRequestDispatcher("ShoppingArea.jsp"); | |
requestDispatcher.forward(request, response); | |
} | |
else { | |
// If authentication fails, we will return the user to the index page (LoginPage) itself | |
response.sendRedirect("index.html"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Servlet code for LoginPage