A simple java servlet which gets request from a web page as parameter, process the request, set response attributes and forwards to destination page
----------------------------------------------
----------------------------------------------
/*
* LoginServlet.java
*
* Created on 23 Dec 2006 Saturday, 07:39
*/
package com.myairline.servlets;
import java.io.*;
import java.net.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.myairline.db.DatabaseConn;
import com.myairline.db.UserDB;
import com.myairline.type.User;
import com.myairline.common.MyConstants;
/**
*
* @author ozkansari
* @version
*/
public class LoginServlet extends HttpServlet {
/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Page will be forwarded to :::
String destinationPage = "";
// Make database connection :::
DatabaseConn dbConn = new DatabaseConn();
Connection connection = dbConn.getConnection();
// get command parameter that will decide which operation is performed :::
String cmd = (String) request.getParameter("cmd");
// get session variable :::
HttpSession sessionVar = request.getSession(true);
// Base Path
String contextPath = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+contextPath+"/";
// Check command and perform the proper operation :::
if( cmd.equals("auth_user") )
{
// Define Variables :::
String userName, password;
UserDB userCheck = new UserDB();
User userCurrent = new User();
int authorizationResult ;
// Get form parameters :::
userCurrent.setUsername( (String) request.getParameter("username") );
userCurrent.setPassword( (String) request.getParameter("password") );
authorizationResult = userCheck.doAuthentication( userCurrent );
if( authorizationResult == MyConstants.LOGGED_IN ) {
sessionVar.setAttribute("userObject",userCurrent);
String rolename = userCurrent.getRolename();
if( rolename.compareToIgnoreCase("customer") == 0 ){
destinationPage = "/customer/customerHome.jsp";
} else if( rolename.compareToIgnoreCase("agency") == 0 ){
destinationPage = "/agency/agencyHome.jsp";
} else if( rolename.compareToIgnoreCase("admin") == 0 ){
destinationPage = "/admin/adminHome.jsp";
}
}
else {
// Set Attribute and destination Page :::
Integer reason = new Integer( authorizationResult );
request.setAttribute( "login", reason );
destinationPage = "/home.jsp";
}
}
// Clean your garbage :::
dbConn.closeConnection();
// Go to destination page :::
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(destinationPage);
dispatcher.forward(request, response);
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
// </editor-fold>
}
Hiç yorum yok:
Yorum Gönder