Hi
I just tried to create the login form using java,jsp and servlets.The database am using here is mysql. The tool am using is eclipse galileo.The code is as follows:
<index.jsp>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<body>
<form action="WelcomeServlet" method="POST">
username <input type=text name=Username>
password <input type=text name=Password>
<input type=submit value=submit>
</form>
</body>
</html>
<WelcomeServlet.java>
package project1.example;
import java.io.IOException;
import java.io.PrintWriter;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class WelcomeServlet
*/
public class WelcomeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("Text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>Welcome</TITLE></HEAD>");
out.println("<BODY BGCOLOR=\"#FFFFFF\">");
out.println("<CENTER>");
out.println("Employees Welcome!!!!");
String user = request.getParameter("Username");
String pass = request.getParameter("Password");
System.out.println(user);
System.out.println(pass);
DbConnection dbconn = new DbConnection();
try{
if(dbconn.validateUser(user, pass)){
//out.println("Jothi" + dbconn.validateUser(user, pass));
//out.println("<jsp:forward page = http://localhost:8084/Project/welcome.jsp");
out.println("Did u come");
}
else{
out.println("><h3>Sorry you are not authorized</h3>");
}
}
catch(Exception e){
e.printStackTrace();
}
out.println("</CENTER>");
out.println("</BODY>");
out.println("</HTML>");
}
}
<DbConnection.java>
package project1.example;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DbConnection {
/**
* @param args
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static Connection getConnection()
throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn=null;
try
{
System.out.println("Before connection");
conn=DriverManager.getConnection("jdbc:mysql:comppromed:3036:anantha","root","buser");
System.out.println("After connection");
}
catch(SQLException e)
{
System.out.println("SQLException: " + e.getMessage());
while((e = e.getNextException()) != null)
System.out.println(e.getMessage());
}
//return conn;
return conn;
}
public static boolean validateUser(String user, String pass)
throws SQLException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException
{
boolean isValid = false;
PreparedStatement stmt=null;
ResultSet rs=null;
String sql = "SELECT USER_NAME, PASSWORD FROM userlogin WHERE USER_NAME= ? AND PASSWORD= ?";
System.out.println( "SQL : " + sql );
System.out.println( "user : " + user );
System.out.println( "pass : " + pass );
stmt = getConnection().prepareStatement(sql);
stmt.setString(1, user);
stmt.setString(2, pass);
stmt.executeQuery();
rs = (ResultSet)stmt.getResultSet();
if(rs.next())
{
return true;
}
return isValid;
}
}
But am getting the error like :
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 7 in the generated java file
The type index_jsp must implement the inherited abstract method Servlet.getServletConfig()
An error occurred at line: 7 in the generated java file
The type index_jsp must implement the inherited abstract method Servlet.service(ServletRequest, ServletResponse)
An error occurred at line: 30 in the generated java file
No exception of type ServletException can be thrown; an exception type must be a subclass of Throwable
An error occurred at line: 44 in the generated java file
The method getPageContext(Servlet, ServletRequest, ServletResponse, String, boolean, int, boolean) in the type JspFactory is not applicable for the arguments (index_jsp, HttpServletRequest, HttpServletResponse, null, boolean, int, boolean)
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:334)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:312)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:299)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:586)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.20 logs.
--------------------------------------------------------------------------------
Apache Tomcat/6.0.2
Please help me with the code.
Thank you.