java rmi, jsp and servlet

i have a jsp form that retrieves information from a database and displays the results on a servlet.instead of a servlet how do i display the details on another jsp page?thx
[193 byte] By [victoriousa] at [2007-11-26 19:04:00]
# 1

> instead of a servlet how do i display the details on

> another jsp page?

This is a question more apt for JSP forum. However, I will try to answer it here. How are you displaying on servlet currently? JSP would be very simliar. Instead of forwarding to servlet, you will now forward to a JSP and inside that JSP <% scriptlet %> you will code to access the same request object (as in servlet) and pull out the attibute values and display them using out.

-BJ

Bimalesha at 2007-7-9 20:51:50 > top of Java-index,Core,Core APIs...
# 2

can u give me an example plz...

my code is as follows...

i have a jsp page as follows that requests information from a database

<body>

<form method="get" action="http://localhost/servlet/JobServlet">

View Job Details:

Enter job number <input name="jobno" type="text" id="jobno" />

<input name="view" type="submit" id="view" value="View Details" />

</form> </body>

my interface:

import java.rmi.*;

public interface JobInt extends Remote {

public String getDetails(int jobno) throws RemoteException; }

the server:

import java.rmi.*;

public class JobServer {

public static void main(String[] args) {

try {

JobImpl j = new JobImpl();

Naming.rebind("//"+"localhost"+"/JobImpl",j); System.out.println("Registered"); }

catch (Exception e) {

System.out.println("Exception occured in JobServer ... " +e); } } }

implementation:

import java.rmi.*;

import java.rmi.server.*;

import java.sql.*;

public class JobImpl extends UnicastRemoteObject implements JobInt {

Connection con; Statement st; ResultSet rs;

String url = "jdbc:mysql://localhost:3306/db?user=root;password=admin";

public JobImpl() throws RemoteException {

super();

try {

Class.forName("com.mysql.jdbc.Driver").newInstance();

con = DriverManager.getConnection(url, "", "");

st = con.createStatement(); }

catch (Exception e) {

System.out.println("problem while establishing connection"+e); } }

public String getDetails(int jobno) throws RemoteException {

try {

rs = st.executeQuery("Select * from job where jobno="+jobno);

rs.next();

return(rs.getString(2)); }

catch (Exception e) {

e.printStackTrace(); }

return null; } }

from the jsp page, when the jobno is entered all the details about the job should be displayed on the servlet

the servlet:

import java.rmi.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

public class JobServlet extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

PrintWriter out = res.getWriter();

res.setContentType("text/plain");

try {

int jobno = Integer.parseInt(req.getParameter("jobno"));

JobInt j = (JobInt)Naming.lookup("//"+"localhost"+"/JobInt");

System.out.println("Job Details: "+j.getDetails(jobno)); }

catch (Exception e1) {

System.out.println(""+e1); } } }

victoriousa at 2007-7-9 20:51:50 > top of Java-index,Core,Core APIs...
# 3

> can u give me an example plz...

I will suggest some simple changes to get you going, later you can experiment to improve the design.

> i have a jsp page as follows that requests

> information from a database

> ...

> <form method="get"

> action="http://localhost/servlet/JobServlet">

Instead of posting to JobServet, you post back to a new JSP, say job_details.jsp (see below).

<form method="get"

action="http://localhost/jsp/job_details.jsp">

/Assuming job_details.jsp is kept at a correct dir under your web app. Rest all things do not change.

> the servlet:

> import java.rmi.*;

> import javax.servlet.*;

> import javax.servlet.http.*;

> import java.io.*;

> public class JobServlet extends HttpServlet {

> public void doGet(HttpServletRequest req,

> HttpServletResponse res) throws ServletException,

> IOException {

> PrintWriter out = res.getWriter();

> res.setContentType("text/plain");

> try {

> int jobno =

> Integer.parseInt(req.getParameter("jobno"));

> JobInt j =

> (JobInt)Naming.lookup("//"+"localhost"+"/JobInt");

> System.out.println("Job Details:

> "+j.getDetails(jobno)); }

> catch (Exception e1) {

> System.out.println(""+e1); } } }

move above code to a new job_details.jsp, something like shown below:

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

...

...

<%

try {

int jobno = Integer.parseInt(request.getParameter("jobno"));

JobInt j = (JobInt)Naming.lookup("//"+"localhost"+"/JobInt");

%>

Job Details: <%= j.getDetails(jobno) %>

<%

}

catch (Exception e1)

{

%>

<%= e1 %>

<%

}

%>

Trust it helps,

-BJ

Bimalesha at 2007-7-9 20:51:50 > top of Java-index,Core,Core APIs...
# 4
i just did that and i have the following error:org.apache.jasper.JasperException: Unable to compile class for JSP
victoriousa at 2007-7-9 20:51:50 > top of Java-index,Core,Core APIs...
# 5

> org.apache.jasper.JasperException: Unable to compile

> class for JSP

Compiler would normally also report the line# of the translated java file. Pls post the complete stacktrace. Since, I had written above snippet w/o an IDE from top of my head, I do not expect it to be free from minor syntax errors. Can you pls check with a decent JSP primer and validate the above syntax. I meant to help you with the concept and not write entire JSP so that you could just copy it as it is :-(

-BJ

Bimalesha at 2007-7-9 20:51:50 > top of Java-index,Core,Core APIs...
# 6

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>

<%@ page import="java.rmi.*" %>

<%@ page import="java.util.*, java.lang.*, java.io.*" %>

<html>

<head>

<title>Untitled Document</title>

</head>

<body>

<%

try {

int jobno = Integer.parseInt(request.getParameter("jobno"));

JobInt e=(JobInt)Naming.lookup("rmi://localhost/job");

out.println("Details for job number "+jobno);

out.println("Registration Date : "+e.getdate(jobno));

out.println("Registration no of vehicle : "+e.getregno(jobno));

} catch (Exception e1) {

out.println(""+e1);

}

%>

</body>

</html>

the exception occurred:

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 13 in the jsp file: /jobdetails.jsp

Generated servlet error:

JobInt cannot be resolved to a type

victoriousa at 2007-7-9 20:51:51 > top of Java-index,Core,Core APIs...
# 7
Hey victorious. Don't offer dukes if you don't intend to pay them.
bckrispia at 2007-7-9 20:51:51 > top of Java-index,Core,Core APIs...
# 8

>

> An error occurred at line: 13 in the jsp file:

> /jobdetails.jsp

> Generated servlet error:

> JobInt cannot be resolved to a type

Pls. import all your remote Interfaces and classes that you are referring in JSP including JobInt and ensure that classes can be found under WEB-INF/classes folder in your web app.

-BJ

Bimalesha at 2007-7-9 20:51:51 > top of Java-index,Core,Core APIs...
# 9
Go to my previous posting http://forum.java.sun.com/thread.jspa?threadID=5122542&messageID=9429772#9429772
The_java_guya at 2007-7-9 20:51:51 > top of Java-index,Core,Core APIs...
# 10

> Pls. import all your remote Interfaces and classes

> that you are referring in JSP including JobInt

> and ensure that classes can be found under

> WEB-INF/classes folder in your web app.

Hi everybody,

As per the example above, can you please specify exactly what "Remote Interfaces" and "Classes" should be imported for the JSP to function properly? And what classes should be included in the WEB-INF/classes folder?

Regards,

Eagle Beta

EagleBetaa at 2007-7-9 20:51:51 > top of Java-index,Core,Core APIs...