Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
    Bookmark Topic Watch Topic
  • New Topic

getting exception during submiting a jsp form

 
syed mahboob
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Report post to moderator


package com.lara.servlets.controller;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Connection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lara.db.pool.ConnectionPool;
import com.lara.model.Action;
import com.lara.model.Bean;
import com.lara.model.RequestInfo;
import com.lara.xml.ModuleConfig;

/**
* Servlet implementation class ControllerServlet
*/
public class ControllerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private ConnectionPool cp=null;
private ModuleConfig moduleConfig=null;


public ControllerServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see Servlet#init(ServletConfig)
*/
public void init() throws ServletException {

String driverClass=getInitParameter("driverClass");
String url=getInitParameter("url");
String username=getInitParameter("username");
String password=getInitParameter("password");
String poolSize=getInitParameter("poolSize");
int intPoolSize=Integer.parseInt(poolSize);
cp=new ConnectionPool(driverClass,url,username,
password,intPoolSize);
cp.init();
// TODO Auto-generated method stub

String fileName=getInitParameter("config-file");
InputStream is = getClass().getResourceAsStream((new StringBuilder("/../")).append(fileName).toString());

try{
moduleConfig=new ModuleConfig(is);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
// TODO Auto-generated method stub


/**
* @see Servlet#destroy()
*/
public void destroy() {
cp.release();
// TODO Auto-generated method stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPerform(request,response);

// TODO Auto-generated method stub
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPerform(request,response);
// TODO Auto-generated method stub
}
private void doPerform(HttpServletRequest request,
HttpServletResponse response) throws ServletException,IOException

{
String path=request.getServletPath();
path=path.substring(1,path.length()-3);
Connection con=cp.checkOut();
request.setAttribute("con",con);
RequestInfo reqInfo=moduleConfig.getRequestInfo(path);
String beanClass=reqInfo.getBeanClass();
Bean beanObj=getBeanObject(beanClass,request);
String actionClass=reqInfo.getActionClass();
Action actionObj=getActionObject(actionClass);
String result=actionObj.process(request, beanObj);
String resultPath=reqInfo.getForward(result);
RequestDispatcher rd=request.getRequestDispatcher(resultPath);
rd.forward(request, response);
request.removeAttribute("con");
cp.checkIn(con);

}
private Bean getBeanObject(String beanClass,HttpServletRequest request)
{
Object obj=null;
Class cls=null;
try
{
cls=Class.forName(beanClass);
obj=cls.newInstance();
}
catch(Exception ex)
{
ex.printStackTrace();
}
Field all[]=cls.getDeclaredFields();
String methodName=null;
Method methodObj=null;
String fieldName;
String fieldValue;
for(Field f1:all)
{
fieldName=f1.getName();
fieldValue=request.getParameter(fieldName);
if(fieldValue!=null)
{
methodName="set"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
try
{
methodObj=cls.getDeclaredMethod(methodName,f1.getType());
if(f1.getType().equals(String.class))
{
methodObj.invoke(obj,fieldValue);
}
if(f1.getType().equals(Byte.class))
{
methodObj.invoke(obj,new Byte(fieldValue));
}
if(f1.getType().equals(Short.class))
{
methodObj.invoke(obj,new Short(fieldValue));
}
if(f1.getType().equals(Integer.class))
{
methodObj.invoke(obj,new Integer(fieldValue));
}
if(f1.getType().equals(Float.class))
{
methodObj.invoke(obj,new Float(fieldValue));
}
if(f1.getType().equals(Long.class))
{
methodObj.invoke(obj,new Long(fieldValue));
}
if(f1.getType().equals(Double.class))
{
methodObj.invoke(obj,new Double(fieldValue));
}
if(f1.getType().equals(byte.class))
{
methodObj.invoke(obj,Byte.parseByte(fieldValue));
}
if(f1.getType().equals(short.class))
{
methodObj.invoke(obj,Short.parseShort(fieldValue));
}
if(f1.getType().equals(int.class))
{
methodObj.invoke(obj,Integer.parseInt(fieldValue));
}
if(f1.getType().equals(long.class))
{
methodObj.invoke(obj,Long.parseLong(fieldValue));
}
if(f1.getType().equals(float.class))
{
methodObj.invoke(obj,Float.parseFloat(fieldValue));
}
if(f1.getType().equals(double.class))
{
methodObj.invoke(obj,Double.parseDouble(fieldValue));
}
if(f1.getType().equals(String[].class))
{
String x[]=request.getParameterValues(fieldName);
methodObj.invoke(obj,new Object[]{x});
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
fieldValue=null;
}
return (Bean)obj;

}



private Action getActionObject(String actionClass)
{
Object obj=null;
try
{
obj=Class.forName(actionClass).newInstance();
}
catch(Exception ex)
{
ex.printStackTrace();
}
return (Action)obj;
}

}





config.xml
------------





<app-config>
<request-info>
<path>login</path>
<bean-class>
com.lara.client.LoginBean
</bean-class>
<action-class>
com.lara.client.LoginBean
</action-class>
<forward name="success">
success.jsp
</forward>
<forward name="failure">
login.jsp
</forward>
</request-info>
<request-info>
<path>reg</path>
<bean-class>
com.lara.client.RegistrationBean
</bean-class>
<action-class>
com.lara.client.RegistrationAction
</action-class>
<forward name="success">
regSuccess.jsp
</forward>
<forward name="failure">
reg.jsp
</forward>
</request-info>
</app-config>



web.xml
---------

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>project</display-name>
<servlet>
<description>
</description>
<display-name>ControllerServlet</display-name>
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>com.lara.servlets.controller.ControllerServlet</servlet-class>
<init-param>
<description>
</description>
<param-name>driverClass</param-name>
<param-value>oracle.jdbc.driver.OracleDriver</param-value>
</init-param>
<init-param>
<description>
</description>
<param-name>url</param-name>
<param-value>jdbcracle:thin:@localhost:1521:XE</param-value>
</init-param>
<init-param>
<description>
</description>
<param-name>username</param-name>
<param-value>test</param-value>
</init-param>
<init-param>
<description>
</description>
<param-name>password</param-name>
<param-value>syed</param-value>
</init-param>
<init-param>
<description>
</description>
<param-name>poolSize</param-name>
<param-value>10</param-value>
</init-param>

<init-param>
<param-name>config-file</param-name>
<param-value>
config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>



<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>


<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>




login.jsp
---------


${status}
<form action="login.do?" method="post">
Username: <input type="text" name="username" value="${param.username}" />
</br> Password: <input type="password" name="password" /> </br> <input
type="submit" value="submit" />

</form>






HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet.init() for servlet ControllerServlet threw exception
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:873)
org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
java.lang.Thread.run(Unknown Source)


root cause

java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
com.lara.servlets.controller.ControllerServlet.init(ControllerServlet.java:46)
javax.servlet.GenericServlet.init(GenericServlet.java:212)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:873)
org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
java.lang.Thread.run(Unknown Source)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.28 logs.


--------------------------------------------------------------------------------

Apache Tomcat/5.5.28
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please TellTheDetails. Where is the exception occurring? And please post the stack trace of the exception. Please UseCodeTags to preserve the formatting.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please continue in the duplicated topic for this question.
 
    Bookmark Topic Watch Topic
  • New Topic