permaculture playing cards*
The moose likes Servlets and the fly likes how can we call valueobject class in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how can we call valueobject class in servlet" Watch "how can we call valueobject class in servlet" New topic
Author

how can we call valueobject class in servlet

jothish chokkalingam
Ranch Hand

Joined: Jan 02, 2006
Posts: 57
In my servlet i called a valueobject file using import statement.....
import ValueObject.*;
But it is showing package not found...
help urgently needed


jothish
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Where did you put the class file?
Also, your classes will need to be in a package.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Please read this.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
jothish chokkalingam
Ranch Hand

Joined: Jan 02, 2006
Posts: 57
i put the class file in C:\Program Files\Apache Group\Tomcat 4.1\webapps\ROOT\WEB-INF\classes
my valueobject code...
package ValueObject;
public class ValueObject
{
String dept;
private String getDept()
{
return dept;
}
private void setDept(String str)
{
dept=str;
}
}
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Do you know what a package is ?
Your package name, with CapitalLetters, is strange and matches your class name.

Where did you put ValueObject.java ?


[My Blog]
All roads lead to JavaRanch
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
Originally posted by jothish chokkalingam:
i put the class file in C:\Program Files\Apache Group\Tomcat 4.1\webapps\ROOT\WEB-INF\classes
my valueobject code...
package ValueObject;
public class ValueObject
{
String dept;
private String getDept()
{
return dept;
}
private void setDept(String str)
{
dept=str;
}
}

You class should be under
C:\Program Files\Apache Group\Tomcat 4.1\webapps\ROOT\WEB-INF\classes\ValueObject and not

C:\Program Files\Apache Group\Tomcat 4.1\webapps\ROOT\WEB-INF\classes\

But its not good to use same class name same package name. And as per coding stndards package name should start with small letter.
jothish chokkalingam
Ranch Hand

Joined: Jan 02, 2006
Posts: 57
nope reddy its not working.....
my valueobject class is in the path...
C:\Program Files\Apache Group\Tomcat 4.1\webapps\ROOT\WEB-INF\classes\value
package value;
public class ValueObject
{
public String dept;
public String getDept()
{
return dept;
}
public void setDept(String str)
{
dept=str;
}
}
the action class is in
C:\Program Files\Apache Group\Tomcat 4.1\webapps\ROOT\WEB-INF\classes
import java.io.*;
import java.lang.*;
import java.net.*;
import java.sql.*;
import java.util.*;
import java.sql.*;
import java.io.File;
import java.util.Date;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.*;
import value.ValueObject.*;
public class SimpleServlet1 extends HttpServlet
{

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
try{
PrintWriter out1 = response.getWriter();
DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
Class.forName ("oracle.jdbc.driver.OracleDriver");
Connection conn=DriverManager.getConnection("jdbc racle:thin:@10.236.8.49:1521:heb","hebuser","hebpass");
// @machineName ort:SID, userid, password
//int i=0,j=0;
String query = "select * from storeevent1";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
out1.println("from servlet");
value.ValueObject v1=new value.ValueObject();




v1.setDept("chenai");
System.out.println(v1.dept);
ServletContext ctx = getServletContext();
System.out.println(ctx);
RequestDispatcher dispatcher = ctx.getRequestDispatcher("/jsp.jsp");

request.setAttribute("data",v1);

dispatcher.forward(request,response);


}
catch(Exception e)
{
e.printStackTrace();
}
}
}
my target jsp page is in C:\Program Files\Apache Group\Tomcat 4.1\webapps\ROOT
<HTML>
<BODY>
<%@ page import="value.ValueObject.*" %>
<%@ page import="java.lang.*" %>
Hello! The time is now <%= new java.util.Date() %>
<% String s=request.getParameter("fyear");
java.lang.Object v2;
v2=request.getAttribute("data");
String dept1=v2.getDept();

%>
<p>
<%=s %>

</p>
<p>
dept
</p>
</BODY>
</HTML>
please help me out .........
Abhinit Saxena
Greenhorn

Joined: Jan 10, 2006
Posts: 18
Hey dude..

what are you doing

your package is value

and your class is ValuePackage

so you can write either

import value.*;


or

import value.ValuePackage;

I hope it will help you out
Abhinit Saxena
Greenhorn

Joined: Jan 10, 2006
Posts: 18
Sorry...replace ValuePackage to ValueObject;
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
import value.ValueObject; (or)
import value.*;


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
Your import statement is wrong.
import value.ValueObject.*;

Use:
import value.ValueObject;

or

import value.*;
Abhinit Saxena
Greenhorn

Joined: Jan 10, 2006
Posts: 18
I noticed one more thing in your code...

you used out.println()
and then forwarding to another page???


Dont you think so it will throw you IllegalStateException???
jothish chokkalingam
Ranch Hand

Joined: Jan 02, 2006
Posts: 57
i didn't type cast the object to my valueobject class....
like
ValueObject v2 = new ValueObject();
v2=(ValueObject)request.getAttribute("data");
Abhinit Saxena
Greenhorn

Joined: Jan 10, 2006
Posts: 18
Remember:
========
your ValueObject class is now in value package..

you have to use it like

value.ValueObject or

<%@ page import="value.ValueObject"%>
.....

.....
ValueObject v2 = new ValueObject();


as your JSP will convert in to servlet and gone to another package (org.apache.....)
jothish chokkalingam
Ranch Hand

Joined: Jan 02, 2006
Posts: 57
ya dudes.....
i got it right
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how can we call valueobject class in servlet