I have one scenario where I want to do some validation before my welcome.jsp page get loaded. Actually the validation is like I have a user id (using request.getRemoteUser) which I want to check in my Db whether it is already there or not. If yes, then I want to redirect it into another page say login.jsp. if not, it will redirect to my welcome.jsp. I have declare one servlet (CheckUser.java) where I am calling doPost from the doGet and also included the same servlet in my welcome.jsp so that before loading it doGet will invoke.
**CheckUser.java**
package DBResource;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CheckUser extends DBConnection {
String page="ListProjects.jsp";
public Connection conn= null;
// This Happens Once and is Reused
@Override
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public boolean SearchUser(String Usrid) throws SQLException{
try{
// Establishing connection using data source
conn=ds.getConnection();
PreparedStatement stmt=conn.prepareStatement("select * from OSS_USER where USER_EMAIL='"+Usrid+"'");
ResultSet rs = stmt.executeQuery();
boolean hasResult = rs.next();
return hasResult;
}
catch (Exception e){
e.printStackTrace();
return false;
}
finally {
if (conn != null) conn.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("on CheckUser doGet...........");
doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("on CheckUser doPost..........");
//String Usrid= request.getRemoteUser();
String Usrid= "abc@tr.com";
contextPath = request.getContextPath();
page = "/ListProjects.jsp";
try {
boolean susr = SearchUser(Usrid);
if (susr==true)
{
response.sendRedirect(contextPath + "/ListProjects.jsp");
}
} catch (SQLException ex) {
Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
}
finally {
if (conn != null) try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Welcome.jsp
<body>
<jsp:include page="/CheckUser" flush="true" />
This issue get Resolved using beans & scriptlet. in the Jsp page i have import my java class(StoreUsrDetails) and declare a jsp use bean (storeusr).
In jsp page
<%@page import="com.thomsonreuters.alpaca.StoreUsrDetails" %>
<jsp:useBean id="storeusr" class="com.thomsonreuters.alpaca.StoreUsrDetails" scope="request" />
using the bean i call a method inside my java method (validateUser) which returns wheather the user details is present in the database or not. after that i have written a scriplet code at the start of my jsp body. And based on the retun value of validateUser method i redirect them to another page.
Jsp Body
<body>
<%
if(storeusr.validateUser(request.getRemoteUser()))
response.sendRedirect("ListProjects.jsp");
%>