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:
  servlet login  Archive Import (harpreet) at 23:33 on Wednesday, May 28, 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
•  Implementing setTimeout causing problems
•  setTimeout for custom objects
•  How to change dynamically the value of CONTENTof META object ?
•  How to change the value of CONTENTof META object ?
•  Onunload event in javascript
•  Re: HTML Launch .exe direct
•  right click+call function
•  Re: ASP Email System Source Code
•  Learning VB(Looking for opinions)


Recent Articles
Simple Thumbnail Solution
Type Anywhere
A Better Moustrap: FmtDate to replace FormatDateTime
ASP.NET Forum Source Code
Internal Search Engine
Javascript Growing Window
Simple date validation
Search engine friendly URLs using ASP.NET (C#.NET)
Function to Return Alpha Characters Only
The OSI Reference Model - A Clear and Concise Illustration !


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2004