Results 1 to 8 of 8
  1. #1
    sandraW is offline Member
    Join Date
    Aug 2010
    Posts
    11
    Rep Power
    0

    Smile finding details in database using JSP and forwards user to a HTML page

    The following HTML displays a form prompting the user to enter a username and a password. On clicking on the Login button, Logon.jsp is called.

    I need to write the code for Logon.jsp, which reads in the userís name and password and checks if this is contained in a database table called registrations.

    If a match is found the user is allowed access to an HTML page called MyHomePage.html, if a match is not found a page stating access denied is displayed.
    :)
    <html>
    <body>
    <br>
    <p><center>Please enter your username and password</center></p>
    <br>
    <form action = "http://localhost:8080/examples/Logon.jsp"
    method = "get">
    <center> username <input type ="text" name = "username"></center><br>
    <center> password <input type ="password" name = "password"> </center><br>
    <center><input type ="submit" name = "submit" value = " Login"> </center>
    </form>
    </body>
    </html>

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by sandraW View Post
    The following HTML displays a form prompting the user to enter a username and a password. On clicking on the Login button, Logon.jsp is called.

    I need to write the code for Logon.jsp, which reads in the userís name and password and checks if this is contained in a database table called registrations.

    If a match is found the user is allowed access to an HTML page called MyHomePage.html, if a match is not found a page stating access denied is displayed.
    :)
    <html>
    <body>
    <br>
    <p><center>Please enter your username and password</center></p>
    <br>
    <form action = "http://localhost:8080/examples/Logon.jsp"
    method = "get">
    <center> username <input type ="text" name = "username"></center><br>
    <center> password <input type ="password" name = "password"> </center><br>
    <center><input type ="submit" name = "submit" value = " Login"> </center>
    </form>
    </body>
    </html>
    A JSP should deal with the presentation of an html page; not with a database layer. It should be left to an ordinary Java class (a bean mayhap?) A Servlet should be ignorant of all that stuff as well. Just create a simple class that checks the user's credentials and returns true or false. If true a session can be created and passed to the JSP, otherwise no session is created. The JSP couldn't and shouldn't care less but base its decision what to display on the presence (or absence) of a session object. Never put any database code in a JSP; it doesn't belong there.

    kind regards,

    Jos

  3. #3
    chimanet is offline Member
    Join Date
    Jun 2010
    Posts
    5
    Rep Power
    0

    Smile

    let me give u a kind of psuedocode
    there are so many ways to perform the task,since you have a html login page wat u do is to create a servlet that u will forward the request to the required page
    the servlet ensures that the request parameters are not null or empty strings
    then get a connection to the database and perform some queries like
    select name from table where password is password .
    if{ resultset.next is true and resultset.name is equal to name then do
    create a Property object p and do p.setProperty("name""value of name")
    get sessionobject and pass p to it as an attribute and get a requestdispatcher object of the logon page and forward it with request and response objects.to the logonpage.jsp}
    else get a requestdispatcher and foward back to the login page.html

    in the jsp logon.jsp use ${p.name} to get the value of the session atribute name

    just follow the above steps and code well you will get it is simple!:)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by chimanet View Post
    then get a connection to the database and perform some queries like
    select name from table where password is password .
    if{ resultset.next is true and resultset.name is equal to name then do
    create a Property object p and do p.setProperty("name""value of name")

    ..snip rest...
    If you're going to give advice at least make sure it's good advice. The check for authentication of a user is a single bit of SQL, which either returns a result or not. There is no need to check the contents of the result.

    Your method above is bizarre...at least the usual mistake makes some degree of sense (select password from users where name = name, then check the password in Java), but selecting based on the password?

    However, both methods are wrong. You select based on both name and password.

  5. #5
    meghana is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default dynamic query in jsp

    hi

    we are developing page that contain checkbox and textbox which are dynamically generated.when we click on check box textbox will be enable.we enter value in textbox.now we want to insert that values in database on click event of button. rest of values should not enter in database, but we are not manupulate dynamic button and check box.

    Source Code:

    <%@page session="false" %>
    <%@page import="java.util.*"%>
    <%@page import="java.sql.*"%>


    <html>
    <head> </head>
    <body>
    <%
    Statement st=null;
    Connection con=null;
    ResultSet rs=null;

    try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    con=DriverManager.getConnection("Jdbc:Odbc:unisoft ");
    st = con.createStatement();
    rs = st.executeQuery("SELECT * from course");
    %>
    <form name="form" method="post" action="">
    <table border="1" align="center" width=650 height=0 cellpadding=8>
    <tr>
    <td colspan=3> <font size=4> Fees Structure <hr> </font> </td>
    </tr>
    <tr>
    <td width=92> Course Name </td>
    <td width="7"> : </td>
    <td width="200"> <select name="course">
    <option value="abc">ABC</option>
    <% while(rs.next())
    {
    %>
    <option value="<%= rs.getString(1)%>"> <%= rs.getString(2) %></option>
    <% } %>
    </select> </td>
    <%
    }
    finally
    {
    if(st!=null) st.close();
    //if(rs!=null) rs.close();
    if(con!=null) con.close();
    }
    %>
    </tr>
    </table>
    <table border="1" align="center" width=650 height=0 cellpadding=8>
    <tr>
    <td width=92> Course Name </td>
    <td width="7"> : </td>
    <td width="200"> <select name="course">
    <option value="Day"> Day </option>
    </select>
    <td>&nbsp; </td>
    </tr>
    <tr align="center">
    <td> <input type="button" name="next" value="Next" style="width:100"> </td>
    <td> <input type="button" name="next" value="Reset" style="width:100"> </td>
    <td> <input type="button" name="next" value="Cancel" style="width:100"> </td>
    <td>&nbsp; </td>
    </tr>
    </table>
    </form>
    </body>
    </html>

    help us to solve that problem.

    thanks

    megha

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Are you sandra in disguise, or are you hijacking someone elses thread?

  7. #7
    chimanet is offline Member
    Join Date
    Jun 2010
    Posts
    5
    Rep Power
    0

    Thumbs up

    @Tolls
    thank you for the constructive criticism, am actually a newbea in programing but i just wanted to help since no body wanted to reply the post.i just have a little idea of SQL,but am sure wat i posted will work but is a little bit of a long process
    thanks toll becpouse i also length some sql from you today like
    select * from table where values name,password

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Ah, but people did reply to the post. This stuff should not be done in a JSP, and many of us will not provide help if people persist in going down this route.

    But yes, your way will work, but it will take more processing (the db is far faster at doing this than Java would be), and is not entirely logical.

Similar Threads

  1. Replies: 1
    Last Post: 03-15-2010, 02:39 AM
  2. what is and how works the first ?, example.com/details?page=4
    By lse123 in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 10-29-2009, 03:18 PM
  3. terminating the page if user sits idle.
    By shivakumari in forum Java Servlet
    Replies: 9
    Last Post: 04-11-2009, 08:46 AM
  4. load graphics in html page using jsp and database
    By charu in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-04-2009, 08:12 PM
  5. HTML page
    By bbq in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 04:46 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •