954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Login page. JSP/Servlet

im creating a website that deals with the monthly reminders (extracted from Oracle) for my members for a school project. Should I create a login page using JSP or servlets? I am unfamiliar in both the languages.
Will I need to establish my connection for each page if I use JSP instead of servlets?

Im confused, please advice.

nisrin
Newbie Poster
18 posts since Jan 2006
Reputation Points: 10
Solved Threads: 0
 

Hi...you can JSP and Servlets or simple Java Classes as normal easy way.
But i would sugeest you to have it done by using Struts......thats the best way out.
You need require to establish connection each time in any of the above cases.
Just have one Java class which will do that for you and let the other classes call the method of that class.

cheers

saurav
Light Poster
28 posts since Jul 2004
Reputation Points: 10
Solved Threads: 2
 

Hello. Thanks for the reply. Programing in Java would have been easier for me. But, Im required to script in either JSP or Servlets.
I suppose I should script in JSP then..How should I retain and extract member details after login, by using sessions??

nisrin
Newbie Poster
18 posts since Jan 2006
Reputation Points: 10
Solved Threads: 0
 

jsp s r actually servlets........they r compiled into servlets........member details u can keep in a db and then do the validation in your java/servlet .....

saurav
Light Poster
28 posts since Jul 2004
Reputation Points: 10
Solved Threads: 2
 

I understand jsp codes get compiled into servlets at the backend. But, for the purpose of my project, I would either script in JSP or manually create Servlets..

Member details will be housed in my db. This is what Im not sure about: The login page takes the user's info and see if it exists in the db. After that the next few pages should use the same user info and track down his set reminders. For that information to be retained in the front-end for JSP usage, should I use sessions??

Thank you very much.

nisrin
Newbie Poster
18 posts since Jan 2006
Reputation Points: 10
Solved Threads: 0
 

yes you are right......u have to use sessions.......
retrieve the details wherever u like

saurav
Light Poster
28 posts since Jul 2004
Reputation Points: 10
Solved Threads: 2
 

Thank you. Now that the theory is clear, hope I can code well.

nisrin
Newbie Poster
18 posts since Jan 2006
Reputation Points: 10
Solved Threads: 0
 

best of the luck..cheers

saurav
Light Poster
28 posts since Jul 2004
Reputation Points: 10
Solved Threads: 2
 

Hi. I don't mean to be a sponger, but I think his project is similar to mine. I am creating a Movie Theatre site for my Web Apps project. I have almost everything done, but I cant figure out how to create a login page. I was told to try a SELECT query and compare them with the data entered, but that isn't working. Here's the code:

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
String url = "jdbc:odbc:userDSN";
String username="";
String password="";
Connection conn=null;
String classPath = "sun.jdbc.odbc.JdbcOdbcDriver";
String usernm = username;
String passwrd = password;
    
try{
    Class.forName(classPath);
    conn = DriverManager.getConnection(url,username,password);
    }catch(Exception exc){
        out.println(exc.toString());
    }
%>
<%
    Statement stm=null;
    ResultSet rst=null;
    
    stm= conn.createStatement();
    String query= "SELECT userID, password FROM user WHERE userID =" +usernm;
    
    rst = stm.executeQuery(query);
%>

<%= rst.getString("userID") %>
<%= rst.getString("password") %>



<% 
if (usernm.equals(userID) && passwrd.equals(password)) { %>

<jsp:forward page = "index.jsp"></jsp:forward>

<% } else { %>

<jsp:forward page = "login_handle.jsp"></jsp:forward>


<% }
            rst.close();
            stm.close();
            conn.close();
%> 
    </body>
</html>


When i run it, Tomcat says :

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 46 in the jsp file: /login_handle.jsp
Generated servlet error:
C:\Documents and Settings\Kemar\.netbeans\5.5.1\apache-tomcat-5.5.17_base\work\Catalina\localhost\Web_Project\org\apache\jsp\login_005fhandle_jsp.java:94: cannot find symbol
symbol  : variable userID
location: class org.apache.jsp.login_005fhandle_jsp
if (usernm.equals(userID) && passwrd.equals(password)) { 
                  ^

I'm not sure where i'm going wrong but i would greatly appreciate your help. I was fortunate enough to stumble across your site. Thanks in advance.

kemarS
Newbie Poster
8 posts since Dec 2007
Reputation Points: 14
Solved Threads: 0
 
Sulley's Boo
Posting Pro in Training
452 posts since Dec 2004
Reputation Points: 529
Solved Threads: 10
 

It did help, somewhat. I made a few amendments to my code but now Tomcat says:

An error occurred at line: 32 in the jsp file: /login_handle.jsp
Generated servlet error:
C:\Documents and Settings\Kemar\.netbeans\5.5.1\apache-tomcat-5.5.17_base\work\Catalina\localhost\Web_Project\org\apache\jsp\login_005fhandle_jsp.java:91: cannot find symbol
symbol  : variable dbuser
location: class org.apache.jsp.login_005fhandle_jsp
if (dbuser.equals(user) && dbpass.equals(pass)){ 
    ^


An error occurred at line: 32 in the jsp file: /login_handle.jsp
Generated servlet error:
C:\Documents and Settings\Kemar\.netbeans\5.5.1\apache-tomcat-5.5.17_base\work\Catalina\localhost\Web_Project\org\apache\jsp\login_005fhandle_jsp.java:91: cannot find symbol
symbol  : variable dbpass
location: class org.apache.jsp.login_005fhandle_jsp
if (dbuser.equals(user) && dbpass.equals(pass)){ 
                           ^

HOWEVER, the code says:

String query= "SELECT userID, password FROM user";
    
    rst = stm.executeQuery(query);
    
     while(rst.next()){ 

     String dbuser = rst.getString("userID");
     String dbpass = rst.getString("password");       

}
if (dbuser.equals(user) && dbpass.equals(pass)){ %>

 <jsp:forward page = "index.jsp" /> 

<% } %>
<%--
<jsp:forward page = "user_login.jsp" />
--%>

<%
            rst.close();
            stm.close();
            conn.close();
%> 
    </body>
</html>


I have the variables declared after it checks to see if the result set has elements. I dont know why it keeps doing this, but further help would be greatly appreciated. Thanks again for the site, it's EXACTLY what i was looking for.

kemarS
Newbie Poster
8 posts since Dec 2007
Reputation Points: 14
Solved Threads: 0
 

Thanks alot. I got through mostly with the project but i could NOT have done it without your help. Where has this site been all my life? lol. Seriously, thanks alot. I appreciate it very much and look forward to helping someone else someday.

kemarS

kemarS
Newbie Poster
8 posts since Dec 2007
Reputation Points: 14
Solved Threads: 0
 

When you try to retrieve information from you rst object, you'll have to do it inside a while loop. For eks.

[<%while(rst.next()){ %> <%= rst.getString("columnName1")%> <%=rst.getString("columnName2")%> <%}%>
Davidjan
Newbie Poster
1 post since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

i need jsp and servlet coding for login page where the username and password must b stored in a db table

ssaatt
Newbie Poster
2 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 
i need jsp and servlet coding for login page where the username and password must b stored in a db table

And I want vacation.
Sorry I thought that this thread was for posting what we want butyou won't get!

Seriously:
This thread is very old. If you have a question start your own. And no one is going to give you the code. We don't give code like that. If you don't have a clue start studying and search for examples in this forum. Post what you have done.
You need to know basic java, connecting to the database and running queries, sql, html and jsp. What do you know from the above?

And separate your methods in different classes. Don't put code that connects to the database in the jsp page.
Once you post what you have done you will get help.

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

yup .. i learnt som servlet and jsp materials .and done the coding part... bt nw d prblm is on clckn d submit button itz not connecting d follwing pages... form actions also i changed ..m a bgnnnr in java ,, pls help me

ssaatt
Newbie Poster
2 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 
yup .. i learnt som servlet and jsp materials .and done the coding part... bt nw d prblm is on clckn d submit button itz not connecting d follwing pages... form actions also i changed ..m a bgnnnr in java ,, pls help me

I didn't understand anything from what you said. And don't expect any answers in this old thread, that the admins should close.

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 


Hi..
Saurv i create a login page on Mysql Database ...so u can change databse according you..

<%-- 
    Document   : Login1
    Created on : Jul 1, 2011, 1:16:12 AM
    Author     : Dharmendra
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%@page import="com.p1.*" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body><center>
            <form action="">
             UserID   :   <input type="text" name="name" value="" size="20" />
             Password :   <input type="password" name="pass" value="" size="20" />
             <input type="submit" value="Login" />
            </form>
        </center>
        
    </body>
</html>
<%
int x=0;
String s1=request.getParameter("name");
String s2=request.getParameter("pass");
out.print(s1+s2);
Connection con=null;
ResultSet rs=null;
PreparedStatement ps=null;
try{
    Class.forName("com.mysql.jdbc.Driver");
    con=DriverManager.getConnection("jdbc:mysql://localhost/dk","root","admin");
    
    String sql="select *from login where userid=? and pass=?";
    ps=con.prepareStatement(sql);
    ps.setString(1, s1);
    ps.setString(2, s2);
    rs=ps.executeQuery();

     if(rs.next()){
        x=1;
        }
    RequestDispatcher rd=null;
    if(x==1){
        out.print("ok");
        rd=request.getRequestDispatcher("home.jsp");
      }
    else{
        out.print("Not ok");
        rd=request.getRequestDispatcher("Login1.jsp");
        }
    rd.forward(request, response);

    }catch(Exception e){
        e.printStackTrace();
        }
finally{
    try{
    rs.close();
    ps.close();
    con.close();
    }catch(Exception ee)
    {
        ee.printStackTrace();
    }
  }

%>
dharma117
Newbie Poster
10 posts since Dec 2009
Reputation Points: 4
Solved Threads: 1
 

Hi.. Saurv i create a login page on Mysql Database ...so u can change databse according you..

<%-- 
    Document   : Login1
    Created on : Jul 1, 2011, 1:16:12 AM
    Author     : Dharmendra
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%@page import="com.p1.*" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body><center>
            <form action="">
             UserID   :   <input type="text" name="name" value="" size="20" />
             Password :   <input type="password" name="pass" value="" size="20" />
             <input type="submit" value="Login" />
            </form>
        </center>
        
    </body>
</html>
<%
int x=0;
String s1=request.getParameter("name");
String s2=request.getParameter("pass");
out.print(s1+s2);
Connection con=null;
ResultSet rs=null;
PreparedStatement ps=null;
try{
    Class.forName("com.mysql.jdbc.Driver");
    con=DriverManager.getConnection("jdbc:mysql://localhost/dk","root","admin");
    
    String sql="select *from login where userid=? and pass=?";
    ps=con.prepareStatement(sql);
    ps.setString(1, s1);
    ps.setString(2, s2);
    rs=ps.executeQuery();

     if(rs.next()){
        x=1;
        }
    RequestDispatcher rd=null;
    if(x==1){
        out.print("ok");
        rd=request.getRequestDispatcher("home.jsp");
      }
    else{
        out.print("Not ok");
        rd=request.getRequestDispatcher("Login1.jsp");
        }
    rd.forward(request, response);

    }catch(Exception e){
        e.printStackTrace();
        }
finally{
    try{
    rs.close();
    ps.close();
    con.close();
    }catch(Exception ee)
    {
        ee.printStackTrace();
    }
  }

%>


Congratulations. You just posted exactly what you shouldNEVER do. You must never write code like that.
-- Not to mention that after a second look your code is wrong!
-- Not to mention that you did the other person's homework.

If ssaatt, you say that you have code post it in a new thread and avoid advice like the above.

No one ever bothers to do some searching? The question asked here has been answered so many times in this forum that there is a sticky post at the beginning of this forum that talks about MVC!

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You