aspose file tools*
The moose likes Servlets and the fly likes i have post all code please help i am still unable to rectify. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Reply locked New topic
Author

i have post all code please help i am still unable to rectify.

syed mahboob
Greenhorn

Joined: Dec 06, 2012
Posts: 15
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;
}

}




HTTP Status 500 -

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

type Exception report

message

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

exception

java.lang.NullPointerException
com.lara.servlets.controller.ControllerServlet.getBeanObject(ControllerServlet.java:124)
com.lara.servlets.controller.ControllerServlet.doPerform(ControllerServlet.java:100)
com.lara.servlets.controller.ControllerServlet.doPost(ControllerServlet.java:87)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)


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


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

Apache Tomcat/5.5.28


package com.lara.client;

import java.sql.Connection;

import javax.servlet.http.HttpServletRequest;

import com.lara.model.Action;
import com.lara.model.Bean;

public class LoginAction extends Action {

public String process(HttpServletRequest request,Bean bean)
{
Connection con=(Connection)request.getAttribute("con");
LoginBean loginBean=(LoginBean) bean;
boolean status=LoginDAO.authenticate(con,loginBean);
if(status)
return "success";
request.setAttribute("param.username","Wrong username or password");
return "failure";
}
}




package com.lara.client;

import com.lara.model.Bean;

public class LoginBean extends Bean {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}


}




package com.lara.client;

import java.sql.Connection;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.lara.db.util.DBUtil;



public class LoginDAO {
public static boolean authenticate(Connection con,LoginBean bean)
{
String username =bean.getUsername();
String password=bean.getPassword();
Statement stmt=null;
ResultSet rs=null;
String sqlQry="";

try{
stmt=con.createStatement();
sqlQry="select * from users where username='"+username+"' and password='"+password+"'";

rs=stmt.executeQuery(sqlQry);
return rs.next();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
finally{
DBUtil.closeResultSet(rs);
DBUtil.closeStatement(stmt);
}
return false;
}

}



package com.lara.model;

import javax.servlet.http.HttpServletRequest;

public class Action {
public String process(HttpServletRequest request,Bean bean)
{
return null;
}
}



package com.lara.model;

public class Bean {

}




package com.lara.model;

import java.util.HashMap;
import java.util.Map;

public class RequestInfo {
private String path;
private String beanClass;
private String actionClass;
private Map<String,String> paths=new HashMap<String,String>();
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getBeanClass() {
return beanClass;
}
public void setBeanClass(String beanClass) {
this.beanClass = beanClass;
}
public String getActionClass() {
return actionClass;
}
public void setActionClass(String actionClass) {
this.actionClass = actionClass;
}
public void putForward(String key,String value)
{
paths.put(key,value);
}
public String getForward(String name)
{
return paths.get(name);
}


}




package com.lara.xml;

import java.io.InputStream;

import java.util.HashMap;


import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.lara.model.RequestInfo;

public class ModuleConfig extends DefaultHandler
{
private HashMap<String,RequestInfo> reqInfos=new HashMap<String,RequestInfo>();
private String content=null;
private RequestInfo reqInfo=null;
private String reqPath=null;
private String forwardName=null;
public ModuleConfig(InputStream is) throws Exception
{
SAXParserFactory sax=SAXParserFactory.newInstance();
SAXParser sp=sax.newSAXParser();
sp.parse(is,this);
}
public void startElement(String uri,String localName,String qName,Attributes attributes) throws SAXException
{
if("request-info".equals(qName))
{
reqInfo=new RequestInfo();
}
if("forward".equals(qName))
{
forwardName=attributes.getValue("name");
}
}
public void characters(char[] ch,int start,int length) throws SAXException
{
content=new String(ch,start,length);
content=content.trim();
}
public void endElement(String uri,String localName,String qName) throws SAXException
{
if("request-info".equals(qName))
{
reqInfos.put(reqPath, reqInfo);
}
if("path".equals(qName))
{
reqInfo.setPath(content);
reqPath=content;
}
if("bean-class".equals(qName))
{
reqInfo.setBeanClass(content);

}

if("action-class".equals(qName))
{
reqInfo.setActionClass(content);

}
if("forward".equals(qName))
{
reqInfo.putForward(forwardName,content);

}
}
public RequestInfo getRequestInfo(String path)
{
return reqInfos.get(path);
}
}






<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>




<?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>


${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>

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

Please do not post the same question more than once. And please UseCodeTags.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: i have post all code please help i am still unable to rectify.