HI I am using Tomcat6.0 this is the problem i got wen i run the code , i am using login.html ,login .jsp,login.java and web.xml code
java.lang.NullPointerException org.apache.jsp.loginjsp.jspInit(login_jsp.java:23) org.apache.jasper.runtime.HttpJspBase.init(HttpJspBase.java:52) org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:159) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:329) 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) // here is code login.jsp <html> <body bgcolor="white"> <h1> I have been invoked by &lt;% out.print(request.getAttribute(&quot;servletName&quot;).toString()); %&gt;</p> <p></body> </html></p> <p>login.java</p> <p>import java.io.IOException; import java.io.PrintWriter;</p> <p>import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;</p> <p>public class Login1 extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { static final long serialVersionUID = 1L;</p> <p>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ response.setContentType(&quot;text/html&quot;); PrintWriter out=response.getWriter(); String name=request.getParameter(&quot;name&quot;); String pass=request.getParameter(&quot;password&quot;);</p> if(name.equalsIgnoreCase("Snipe")&& pass.equals("snipe")){ // getServletContext().getRequestDispatcher("/login.jsp").forward(request,response); response.sendRedirect("/Simple/jsp/login.jsp"); }else{ out.println("<html>"); out.println("<body>"); out.println("Invalid User name OR passowrds"); out.println("</body>"); out.println("</html>"); } }catch (Exception e) { e.printStackTrace(); } } } // html file is: <!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>User Login</title> </head> <body bgcolor="cream"> <form action="login" method="get"> Enter User name: <input type="text" name="name"><br></p> <p>Enter Password: <input type="password" name="password"><br></p> <p><input type="submit" name="submit" value="login"></p> <p></body> </html> and web.xml code: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>ActivityManagementSystem</display-name> <display-name>Activity Management System</display-name> <description> Welcome to Web Appllication </description> <welcome-file-list> <welcome-file>html/Login.html</welcome-file> </welcome-file-list> <servlet> <servlet-name>ZmgrVal</servlet-name> <servlet-class>servletapplication.Login1</servlet-class> </servlet> <servlet-mapping> <servlet-name>ZmgrVal</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> </web-app>
Does anyone have a clear resolution to this vexing issue? Thanks in advance.
Hello Friend,
Try this:
1)form.html:
<html> <form method="post" action="http://localhost:8080/examples/jsp/login.jsp"> <table> <tr><td>Name:</td><td><input type="text" name="name"></td></tr> <tr><td>Address:</td><td><input type="password" name="password"></td></tr> <tr><td></td><td><input type="submit" value="Submit"></td></tr> </table> </form> </html>
2)login.jsp:
<% String name = request.getParameter( "name" ); String pass = request.getParameter( "password" ); request.setAttribute( "theName", name ); request.setAttribute( "thePass", pass ); %> <jsp:forward page="../Login1"/>
3)Login1.java:
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.*; import javax.servlet.http.*; public class Login1 extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ response.setContentType("text/html"); PrintWriter out=response.getWriter(); String name=(String)request.getAttribute("theName"); String pass=(String)request.getAttribute("thePass") ; System.out.println(name+" "+pass); if(name.equalsIgnoreCase("Snipe")&& pass.equals("snipe")){ response.sendRedirect("/examples/jsp/form.html"); }else{ out.println("<html>"); out.println("<body>"); out.println("Invalid User name OR passwords"); out.println("</body>"); out.println("</html>"); } }catch (Exception e) { e.printStackTrace(); } } }
4)web.xml:
Put the following inside web-app tag:
<servlet> <servlet-name>Login1</servlet-name> <servlet-class>Login1</servlet-class> </servlet> <servlet-mapping> <servlet-name>Login1</servlet-name> <url-pattern>/Login1</url-pattern> </servlet-mapping>
Thanks
Ads