codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  trying to verify user id & passwd frm database using servlet and jsp  Archive Import (harpreet) at 04:35 on Thursday, May 29, 2003
 

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);
}
}









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  For better design, please vote!
•  Re: dynamic crystal report generation
•  Finding File Size and Creating Error log
•  Ajax example: online chat
•  Re: Nested Javascripts
•  ISP says linefeeds are causing problems
•  Linker Error Unresolved External Borland Builder 2006
•  Have multiple ASP.NET web projects share pages and controls
•  Re: How can I read ASCII data file in C++


Recent Articles
What is a pointer in C?
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net


© Copyright codetoad.com 2001-2006