|
I`m a newbie with jsp and have encountered this problem....i want a login page with user id and password which refers to mysql databse to retrieve the user id and check if the id exist and if the password matches....i`ve also created 2 jsp pages accordingly for the linking..the student id and passwrd r stored in table called student under the values dbStudentId & dbStudPasswd respectively...the follwoing is the rubbish i`ve come up wit...!
package cwservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class StudentLogin extends HttpServlet
{
private String bnDbDriver="org.gjt.mm.mysql.Driver";
private String bnDbId="jdbc:mysql://localhost/bsc_47db";
private String bnDbUser="bsc_47";
private String bnDbPassword="bsc_47";
public String getIdPwd()
{
String returnString="";
try
{
//***********************
// get the connection
//***********************
Class.forName(bnDbDriver);
Connection myConnection=DriverManager.getConnection(bnDbId,bnDbUser,bnDbPassword);
//**********************
// do the SQL query
//**********************
Statement myStatement=myConnection.createStatement();
String sqlQuery="SELECT dbStudent Id, dbStudPasswd from student";
ResultSet IdPwdRS=myStatement.executeQuery(sqlQuery);
while(IdPwdRS.next())
{
returnString+="<OPTION value=`"+IdPwdRS.getString("dbStudent")+"`>"+IdPwdRS.getString("dbStudPasswd");
}
}
catch(Exception e)
{
return ""+e;
}
return returnString;
}
//======== END OF SQL ============================================================
//================================================================================
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response); //handle GET and POST the same way
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
String studentId=request.getParameter("frmStudentId");
String studentPwd=request.getParameter("frmStudentPwd");
System.out.println("LoginServlet.doGet(): studentId="+studentId);
if ((studentId !=null) && studentPwd.equals("qwerty"))
{
//Valid
forwardTo("/cw/jsp/studentlogin.html",request,response);
}
else
{
//try again
forwardTo("/cw/jsp/loginsuccess.html",request,response);
}
}
private void forwardTo(String url,
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
RequestDispatcher myReqDisp;
myReqDisp = getServletContext().getRequestDispatcher(url);
myReqDisp.forward(request, response);
}
}
|
|
|
|
|
|
|
|