0

This is the emp-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
   <context:component-scan base-package="com.cgi.controller"/>  
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/JSPS/" />
      <property name="suffix" value=".jsp" />
   </bean>                  
   <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
     <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
     <property name="url" value="jdbc:mysql://localhost:3306/tutorial" />  
     <property name="username" value="root" />  
     <property name="password" value="tiger" />  
   </bean>  
   <bean id="mysessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
     <property name="dataSource" ref="ds"></property>  
     <property name="mappingResources">  
       <list><value>Employee.hbm.xml</value></list>  
     </property>  
     <property name="hibernateProperties">  
       <props>  
         <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
         <prop key="hibernate.hbm2ddl.auto">update</prop>  
         <prop key="hibernate.show_sql">true</prop>     
       </props>  
     </property>  
    </bean> 
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> 
      <property name="sessionFactory" ref="mysessionFactory"/>
    </bean>
    <bean id="LoginDao" class="com.cgi.dao.LoginDaoImpl">  
       <property name="ht" ref="hibernateTemplate"></property>  
    </bean>  
</beans>  

This is the web.xml;

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Login Example</display-name>
  <welcome-file-list>
    <welcome-file>Login-Form.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>emp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/Controller-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>emp</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/emp-servlet.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

And this is my controller package com.cgi.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.cgi.beans.Employee;
import com.cgi.dao.LoginDaoImpl;

import java.util.*;

@Controller
public class EmployeeController
{
    LoginDaoImpl d;

    @RequestMapping(value="/Check", method=RequestMethod.POST)
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception 
    {
        String name=request.getParameter("username");
        String password=request.getParameter("pass");
        List<Employee> b=(List<Employee>) d.checkuser(name, password);
        System.out.println("Hello");
        Map m=new HashMap();
        m.put("msg", "Hello"+name);

        if(b.size()>0)
        {
            return new ModelAndView("Success",m);
        }
        else
        {
            return new ModelAndView("Fail");
        }
    }

    @RequestMapping(value="/GetAll", method = RequestMethod.POST)
    public ModelAndView getAll(HttpServletRequest request, HttpServletResponse response) throws Exception 
    {
        List<Employee> l=d.getall();

        Map m=new HashMap();
        m.put("msg", l);
        return new ModelAndView("AllUsers",m);
    }

    @RequestMapping("/Register.htm")
    public ModelAndView register(HttpServletRequest req, HttpServletResponse resp) throws Exception
    {
        ApplicationContext ax=new ClassPathXmlApplicationContext("emp-servlet.xml");

        Employee e=(Employee)ax.getBean("d");  
        d.save(e);

        return new ModelAndView("AllUsers");
    }
}

And this is the jsp file

<%@ 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>Login Form</title>

</head>
<body>

<form method="post" action="/emp/Check"> 
<center>
<pre>

User-Name: <input id="name" type="text" name="username" required ><br>
Password:  <input id="pswd" type="password" name="pass" required><br>

<input type="submit" value="Submit"><br>

</pre>
</center>
</form> 
</body>
</html>

The problem is that from jsp it is noyt going to my controller although I have the right mapping scheme in web.xml. I have checked my sites but they all show the same. Please if someone could run it on their system and check it .. Please revert asap.

Error Report .

SEVERE: Servlet.service() for servlet [emp] in context with path [/Employee_Management_Tool] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException at com.cgi.controller.EmployeeController.handleRequest(EmployeeController.java:28) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563) at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source)

Priyash Maini
  • 15
  • 1
  • 7
  • 1
    Check ur log are u getting something like : No mapping found for HTTP request with URI [/registrationModule/xxxx] in DispatcherServlet with name 'xxxx' – udit khare Jan 29 '16 at 12:25

1 Answers1

1
  1. check if u r jsp file is in '/WEB-INF/JSPS/' folder.
  2. check your web.xml, there 'contextConfigLocation' is mentioned twice. see below

    <servlet>
    <servlet-name>emp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/Controller-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    

and again with another value

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/emp-servlet.xml</param-value>
  </context-param>

I think u should remove the second one and correct the first one with the value '/WEB-INF/emp-servlet.xml'.

  1. Check u r action in jsp 'action="/emp/Check', it should be like 'action=Check'
vineeth sivan
  • 510
  • 3
  • 18
  • The login-form.jsp is in web content and the view that is the success page is in JSPS folder. Yes, The 2nd and 3rd point i got, I corrected it, This error is showing up now – Priyash Maini Jan 29 '16 at 12:18
  • I have not got it correctly, good if you can add the full stacktrace of the error. Please check the below link in stckoverflow ,if that would help 'http://stackoverflow.com/questions/2322031/why-did-servlet-service-for-servlet-jsp-throw-this-exception'. – vineeth sivan Jan 29 '16 at 12:26
  • The login-form.jsp is in web content and the view that is the success page is in JSPS folder. Yes, The 2nd and 3rd point i got, I corrected it, This error is showing up now showing null pointer exception. Something like this ......................................................................................................................Servlet.service() for servlet [emp] in context with path [/Employee_Management_Tool] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause – Priyash Maini Jan 29 '16 at 12:27
  • please add the root cause of the error by editing your post I can't see it in comment. – vineeth sivan Jan 29 '16 at 12:35
  • I think you are getting a null pointer exception in the controller class. It may be because the autowiring is not happening so the object of LoginDaoImpl is not created. Please try to work on that. For more info please see the following link 'http://stackoverflow.com/questions/14491093/javaspring-severe-servletservice'. – vineeth sivan Jan 29 '16 at 15:59