rmi, jsp + mysql

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

<body>

<form method="post" 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>

</html>

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

}

}

}

i get a HTTP 405 error... i really dont know whats the problem and y it doesnt work... im doing an assignment on these... plz help

[2975 byte] By [victoriousa] at [2007-11-26 18:24:09]
# 1

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

<body>

<form method="post" 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); } } }

i get a HTTP 405 error... i really dont know whats the problem and y it doesnt work... im doing an assignment on these... plz help

victoriousa at 2007-7-9 5:58:13 > top of Java-index,Core,Core APIs...
# 2
Your servlet only accepts HTTP GET requests, but your JSP is trying to do an HTTP POST on the form.
bckrispia at 2007-7-9 5:58:13 > top of Java-index,Core,Core APIs...
# 3
oh ****!!thx!! and i only need to change the Get method to Post for it to work?
victoriousa at 2007-7-9 5:58:13 > top of Java-index,Core,Core APIs...
# 4
^ Well, try it.
bckrispia at 2007-7-9 5:58:13 > top of Java-index,Core,Core APIs...
# 5
i've tried it... i changed the form method to "get" instead of "post"...when i submit the form, it goes to the servlet but the servlet is just blank... it displays nothing...
victoriousa at 2007-7-9 5:58:13 > top of Java-index,Core,Core APIs...
# 6
Umm, maybe because your servlet never actually writes anything back to its output stream?
bckrispia at 2007-7-9 5:58:13 > top of Java-index,Core,Core APIs...
# 7
hmm... can u explain how i should do it plz...i want my servlet to display the details about a particular job...thx a lot
victoriousa at 2007-7-9 5:58:13 > top of Java-index,Core,Core APIs...
# 8
You have a PrintWriter named 'out', but you send your output to 'System.out' *big* difference.Feed me dukes!!
bckrispia at 2007-7-9 5:58:13 > top of Java-index,Core,Core APIs...
# 9
*ahem*
bckrispia at 2007-7-9 5:58:13 > top of Java-index,Core,Core APIs...