• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

html page and servlet interaction Kindly help

 
nash avin
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
This is my login.html page

<HTML>
<head>
<title>Login page </title>
</head>
<BODY>
<br>
<p> <h3><center>Please enter your user name and password</center></h3></p>
<br>
<br>
<center>username</center>
<center><input type = "text" name="username"></center>
<center>password</center>
<center><input type = "password" name ="password"></center>
<form action="/sampleapp/AuthenticateServlet2" method="post">
<center><input type="submit" name="Submit"value="submit"></center>
</form>
</BODY>
</HTML>
This is my AuthenticateServlet2.java servlet
/********************CODE*************************/
package package1.structure;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;

public class AuthenticateServlet2 extends HttpServlet {
private String user="";
private String pass="";
private String username="";
private String password="";
public void init() {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver loaded successfully");

}//close try
catch (ClassNotFoundException e) {
System.out.println(e.toString());
}//close catch

}//close init() method

public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
{}//close doGet
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
user = request.getParameter("username").trim();
pass = request.getParameter("password").trim();
System.out.println("*"+user+"*");
System.out.println("*"+pass+"*");
String message = null;
try{
Connection con= DriverManager.getConnection("jdbc:mysql:MyDS");
System.out.println("Successfully connected to database");
Statement stmt = con.createStatement();
String sql = "SELECT username,password FROM user" +
" WHERE username ='" + user + "'" +
" AND password='" + pass + "'";
ResultSet rs = stmt.executeQuery(sql);
username = rs.getString(2); //2 is the number of the column that the username is in
password = rs.getString(3);
if (username != null && password != null && user.equals(username) && pass.equals(password) ){
message ="ok";
PrintWriter out = response.getWriter();
out.print("<B>" + message + "</B>");
//response.sendRedirect ("http://localhost:8080/thankyou.html");

}//closes the if statement
else {
response.sendRedirect ("http://localhost:8080/sampleapp/login.html");

}//close else statement
}//closes 2ndtry
catch (SQLException e) {
System.out.println(e.toString());

}//closes 2ndcatch

}//close doPost

}//close class

I am getting the following errors in the web browser on invoking this servlet
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
package1.structure.AuthenticateServlet2.doPost(AuthenticateServlet2.java:34)
javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

Thanking you
AS
 
Moorthi Ramasamy
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by nash avin:

<form>
<center>username</center>
<center><input type = "text" name="username"></center>
<center>password</center>
<center><input type = "password" name ="password"></center>
<form action="/sampleapp/AuthenticateServlet2" method="post">
<center><input type="submit" name="Submit"value="submit"></center>
</form>
user = request.getParameter("username").trim();
pass = request.getParameter("password").trim();
AS

Nash,
You are getting NullPointerException @ request.getParameter("username"). The reason is that you are not getting the "username" & "password" values from browser. Modify your HTML form to include the username & password inside the form.
Thanks,
Moorthi
 
nash avin
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
thanks for the heads up regarding the cause of the problem.I will try and figure out how this can be done.Will keep posting for further questions.
Thanks again
Nash
 
nash avin
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Posting again,would be possible if I could get a heads up regarding how the username and password will be taken from the browser.Hope that it is not asking for too much.
Thanks in advance
nash
 
jayant shanker
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the html form where u r doing "POST" instaed do "GET" and see when the submit is been called in the url just see whether the username and password fields are been passed to the servlet with the query string or just call the servlet independently and passing the username and password fields as a query string.
 
nash avin
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for the message.I did change the message from "post" to "get" and clicked on the submit button.
The next page is a blank page and the url that is being displayed is....
AuthenticateServlet2?username=&password=&Submit=submit.
Kindly let me know what next has to be done.I didnt quite get your response on the message.
Help would be appreciated
Thanks
AS
 
Basavaraju Banakar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The form u are submitting does not include the Username and password input elements... they are outside the <form> </form> tag ... modify this to include them within these tags in HTML...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic