Hi..

i need to learn how to send the username and password to the database and when the user logs in it should retrieve the password from the database and authenticate if the username and password is correct.

Am using this jsp code and getting error in (Resultset line)
here i attached my connection program

<%@ page import="javax.sql.*"%>
<%!
	Connection cn=null;
	Statement st=null;
%>
	<%
		Class.forName("com.mysql.jdbc.Driver");
		cn=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3307/legal_aid", "root", "123456");
	  
	   out.println("Connection successfully completed...");
		
		
	%>

here i attached jsp code for comparision

<%@ include file="getcon.jsp"%>
<%@ page import="java.sql.DriverManager"%>
<%@ page import="java.sql.ResultSet"%>
<%@ page import="java.io.IOException"%>
<%@ page import="com.mysql.jdbc.Connection"%>
<%@ page import="com.mysql.jdbc.*"%>
<%@ page session="true"%>
<%!String s1,p1;
  ResultSet rs=null;%>
   
 <%s1=request.getParameter("username");
p1=request.getParameter("password");


 rs=st.executeQuery("select * from registration where user_name='"+s1+"' and password='"+p1+"'");
 
 
 if(rs.next())
 {%>
 response.sendRedirect("service_upload.html");

<%}

else
{
%>
response.sendRedirect("login.html");
<%}%>

This is am getting error


org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:498)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

root cause

java.lang.NullPointerException
org.apache.jsp.legal_005faid.login_jsp._jspService(login_jsp.java:86)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

Please give me the solution for this
or else if u have any other related code send me to my email-id.

Thanking you.
madhu ms
<EMAIL SNIPPED>

Recommended Answers

All 3 Replies

First of all you don't initialize the Statement object. You have your connection and then you call the execute, but you didn't create the st.

Also everything you are doing is wrong. And don't tell me that it is correct because it executes. Don't open connection or run queries in a jsp. Have a separate method that returns true or false in a separate class. In the method you will open the connection, run the query, close the connection, which you don't, end return the result.
Then you will call that in the jsp.

ok. if u have related code. please send me....

ok. if u have related code. please send me....

You already have the code. The problem is that the st is null. You don't initialize it. Check some tutorials or the java API of the Connection class. There is a method that creates a Statement object.

Also since you already have the code, just create a class and put the validation code inside it as instructed. With the code that you have surely you can put it into a method and call that method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.