Other method inside HttpServlet class
843840Jul 24 2002 — edited Jul 26 2002Here I just declared a metod inside the HttpServlet class, why is it not accepting, it says i can't declare a method in that way?..why.If not can you please tell me the correct way of doing that?.I just want to write validateUser method which will validate aginst the datatabase and retruns either NULL(if the username/pass wouldn't match) or else the user name to the servlet. Please help me...as soon as possible thank you.
package isispack;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;
import java.sql.SQLException;
public class Login extends HttpServlet{
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException{
//this method checks for the USERNAME and Password combination in the database
//and returns the username,if it can't find it will return null.
String userId = req.getParameter("user_id");
String password = req.getParameter("user_pass");
String uName;
//ValidateUser Validate = new ValidateUser();
String ValidateUser(String inputUserid, String inputPwd) throws SQLException{
String returnString = null;
String dbUserid = "isis"; // Database user id
String dbPassword = "osiris" ; // Database password
try{
Class.forName("oracle.jdbc.driver.OracleDriver");//load and Regiser the Oracle Driver
//Driver driver = (Driver)
//Class.forName("OracleDriver").newInstance();
//DriverManager.registerDriver(driver);
Connection con = DriverManager.getConnection("jdbc:oracle:thin:WDCORCL01DEV:1521:USAD","isis","osiris");
//con.getconnection
//create statement object
Statement stmt = con.createStatement();
//execute SQL query amd get Result Set
String sql= "select user_id from isis_table where user_id = '" +
inputUserid + "' and user_pass= '" + inputPwd +"' ;" ;
ResultSet rs = stmt.executeQuery(sql);
if (rs.next())
{
returnString = rs.getString("user_id");
}
stmt.close();
con.close();
return returnString ;
}catch(ClassNotFoundException cfe){};
//}catch(IlligalAccessException ile){};
}
// if uName is null .. user is not authorized.
try{
uName = ValidateUser(userId, password);
if (uName == null)
{
PrintWriter out = res.getWriter( );
out.println("<HTML>");
out.println("<HEAD>");
out.println("<BODY BGCOLOR=\"white\">");
// HTML for this page
out.println("<TITLE>Your Personal Random Numbers</TITLE>");
out.println("<H1>Your Personal Random Numbers</h2>");
out.println("<P>Here are your personal random numbers,");
out.println("</BODY>");
out.println("</head>");
out.println("</html>");
out.close();
}
else
{
//- So the user is valid let's create a seesion // for this user.
PrintWriter pt = res.getWriter();
pt.println(" needn't verify the Username and password");
pt.close();
HttpSession userSession = req.getSession(true);
//put the user name session variable.
userSession.putValue("userName", uName);
//now we need to transfer the control to welcome.jsp
RequestDispatcher rd =
getServletContext().getRequestDispatcher("/success.jsp");
if (rd != null)
{
rd.forward(req,res);
}
}
}catch( Exception e){}
}// end of doPost
}// end of servlet clas