my web application is ruing but when I submit the html form data, the galsfish server4.1.1 throws an exception -java.lang.NullPointerException
. My servlet code:
package com.amir;
import java.sql.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.*;
public class LoginServelet extends HttpServlet {
Coection co;
@Override
public void init(ServletConfig config) throws ServletException
{
super.init( getServletConfig());
try
{
Class.forName("com.mysql.jdbc.Driver");
co = DriverManager.getCoection("jdbc:mysql://localhost/studentdb", "root", "mysql");
}
catch(ClassNotFoundException e)
{
System.out.println("Error(class): "+e);
}//end driver loading
catch(SQLException e)
{
System.out.println("Error(sql):"+e);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException
{
response.setContentType("text/html");
PrintWriter output = response.getWriter();
try
{
String query = "select * from login";
Statement stmt= co.prepareCall(query);
ResultSet rs = stmt.executeQuery(query) ;
String useame = request.getParameter("name");
String password = request.getParameter("pass");
int f=0;
while(rs.next())
{
if(useame.equals(rs.getString("useame")) && (password.equals(rs.getString("password"))))
{
System.out.println("logIn Successful!");
output.println("<html>");
output.println("<head>");
output.println("<title>LogIn</title>");
output.println("</head>");
output.println("<body>");
output.println("<h1>logged in</h1>");
output.println("</body>");
output.println("</html>");
System.out.println("login successful");
f=1;
break;
}
}
if(f!=1)
{
output.println("<html>");
output.println("<head>");
output.println("</head>");
output.println("<body>");
output.println("<h1>log in fail!</h1>");
output.println("</body>");
output.println("</html>");
}
}
catch(Exception e)
{
System.out.println("Error(sql):"+e);
}
}
}
My html file:
logIn page
</head>
<body>
<center>
<fieldset>
<legend>LogIn</legend>
<form action="LoginServelet" method="post" target="_blank">
useame: <input type="text" name="user" value=""/><br>
password: <input type="password" name="pass" value=""/><br>
<input type="submit" value="enter" /><input type="reset" value="clear"/>
New User?<a href="register.html">Register</a>
</form>
</fieldset>
</center>
</body>
My web.xml file is:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>LoginServelet</servlet-name>
<servlet-class>com.amir.LoginServelet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServelet</servlet-name>
<url-patte>/LoginServelet</url-patte>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
how do I get coection to database SQLserver5.7.xx ?

