java - Spring mvc with jdbc template -
i want implements simple login spring mvc , jdbc template. getting null pointer exception. have included spring-core.jar,spring-jdbc.jar,spring-txn.jar,spring-web.jar..
****complete stack trace:**** may 17, 2015 6:54:54 pm org.apache.catalina.core.standardwrappervalve invoke severe: servlet.service() servlet [login] in context path [/mvcwithjdbc] threw exception [request processing failed; nested exception java.lang.nullpointerexception] root cause java.lang.nullpointerexception @ com.mvc.dao.logindaoimpl.getlogindetails(logindaoimpl.java:25) @ com.mvc.controller.logincontroller.handlerequest(logincontroller.java:30) @ org.springframework.web.servlet.mvc.simplecontrollerhandleradapter.handle(simplecontrollerhandleradapter.java:48) @ org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:771) @ org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:716) @ org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:647) @ org.springframework.web.servlet.frameworkservlet.dopost(frameworkservlet.java:563) @ javax.servlet.http.httpservlet.service(httpservlet.java:646) @ javax.servlet.http.httpservlet.service(httpservlet.java:727) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:303) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) @ org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:241) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:220) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:122) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:504) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:170) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:103) @ org.apache.catalina.valves.accesslogvalve.invoke(accesslogvalve.java:950) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:116) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:421) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1074) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:611) @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:316) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1145) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:615) @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) @ java.lang.thread.run(thread.java:745) **web.xml:** <?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" 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>mvcmultpleactions</display-name> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>login</servlet-name> <servlet-class> org.springframework.web.servlet.dispatcherservlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>login</servlet-name> <url-pattern>*.ak</url-pattern> </servlet-mapping> </web-app> **login-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" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean name="/login.ak" class="com.mvc.controller.logincontroller"> </bean> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/web-inf/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="ds" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> <property name="driverclassname" value="oracle.jdbc.driver.oracledriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" /> <property name="username" value="system" /> <property name="password" value="mysql" /> </bean> <bean id="logindao" class="com.mvc.dao.logindaoimpl"> <property name="datasource" ref="ds"></property> </bean> </beans> **dao classes:** package com.mvc.dao; import javax.sql.datasource; import org.springframework.beans.factory.annotation.autowired; import org.springframework.jdbc.core.jdbctemplate; import org.springframework.jdbc.core.support.jdbcdaosupport; import org.springframework.jdbc.datasource.drivermanagerdatasource; import com.mvc.mapper.*; import com.mvc.dto.logindto; public class logindaoimpl extends jdbcdaosupport implements logindao { @override public logindto getlogindetails(string username) { // todo auto-generated method stub system.out.println("username:"+username); string query="select password login username=?"; system.out.println("query"+query); return getjdbctemplate.queryforobject(query,new object[]{username},new loginmapper()); } } **controller:** package com.mvc.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.web.servlet.modelandview; import org.springframework.web.servlet.mvc.controller; import com.mvc.dao.logindaoimpl; import com.mvc.dto.logindto; public class logincontroller implements controller{ @override public modelandview handlerequest(httpservletrequest req, httpservletresponse res) throws exception { // todo auto-generated method stub string name=req.getparameter("username"); string password=req.getparameter("password"); logindaoimpl login=new logindaoimpl(); logindto logindto=new logindto(); logindto.setusername(name); logindto.setpassword(password); system.out.println("pw:"+password); string pw=login.getlogindetails("admin").getpassword(); system.out.println("dao pw:"+pw); if(password.equalsignorecase(pw)) { string message = "hello "+name; return new modelandview("hellopage", "message", message); } else{ return new modelandview("errorpage", "message","sorry, username or password error"); } } }
you can extend jdbcdaosupport
public class logindaoimpl extends jdbcdaosupport implements logindao { //codes }
while jdbctemplate
setter method can removed implementation class.
and can see tutorial reference of answer.
Comments
Post a Comment