-2

I just want to call jsp page from simple java class where i don' t have any request objects. without using any servlet. Just forward to jsp page from java class.

Rajesh Acharya
  • 55
  • 1
  • 2
  • 14
  • Forward what??? You say: "where i don' t have any request objects" but request and response are implicit objects, you will always have them. So what do you want to do? – Himanshu Bhardwaj Apr 01 '13 at 10:08
  • 1
    actually, all `.jsp` files are complied to servlets, so, to call it, you should have `HttpRequest` and `HttpResponse` objects – bsiamionau Apr 01 '13 at 10:11
  • make a HTTPGet or HTTPPost call – Hussain Akhtar Wahid 'Ghouri' Apr 01 '13 at 10:12
  • hi Himanshu, thanks for the quick reply, let me tell you my friend. all i am trying to do is parsing xml file into mysql and then i want to draw bar chart in jsp to the whatever contents in mysql. So now i am able to insert into mysql, i am also able to draw bar chart as well but not in jsp page. so i have to give the connection to jsp page from java class, i want the control should go to jsp page as soon as it is done with the inserting. – Rajesh Acharya Apr 01 '13 at 10:15
  • I am not sure what you are looking for but you can try URL class to call jsp page as you have not response object. – commit Apr 01 '13 at 10:24
  • how can i use URL class, can you please explain that with an example? – Rajesh Acharya Apr 02 '13 at 11:01

2 Answers2

2

First to call a java class from JSP page: you need to instantiate an instance from this class.

For example: if you have a class called "myclass" and a JSP called "home.jsp" then in your JSP page import the myclass ex, <@ page import="yourpackagename.yourclassname"> then in the body part instantiate an instance from your class by typing my1.callyourfunction(); as follow:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="yourpackagename.myclass"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>home.jsp</title>
</head>
<body>
    <%
         myclass my1 = new myclass();
         my1.Openpage(response);
    %>
</body>
</html>

Second to call a jsp from a java class: you need to use HttpServletResponse such as the following:

package yourpackagename.myclass;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

public class myclass{   
public void Openpage(HttpServletResponse res) throws IOException{
    // here type your JSP page that you want to open
    res.sendRedirect("To.jsp");
    }
}
Areeg Samir
  • 577
  • 5
  • 6
1

If i am not mis understood, you are looking for JSP page to be opened in a browser via java class? if yes, you can use Desktop API.

You can also look into following answers:

Open local html page - java

Getting java gui to open a webpage in web browser

Also keep in mind that your JSP page should be placed in a web container(Tomcat etc.) and its running when invoked OR you will be stuck finding out Why JSP is not opening.

Community
  • 1
  • 1
usman
  • 1,351
  • 5
  • 23
  • 47
  • how can i use it, please explain me bit more, – Rajesh Acharya Apr 01 '13 at 10:23
  • yes, it is working now, but i want it to be opened not on the browser, since this is java application, i just want it to be opened in eclipse itself..how it can be done sir? – Rajesh Acharya Apr 01 '13 at 10:32
  • simple, follow steps in 2nd link if not try your self with following code : public static void main(String[] args) { try { //Set your page url in this string. For eg, I m using URL for Google Search engine String url = "http://www.google.com"; java.awt.Desktop.getDesktop().browse(java.net.URI.create(url)); } catch (java.io.IOException e) { System.out.println(e.getMessage()); } } } – usman Apr 01 '13 at 10:32
  • you mean its Stand alone/ Desktop java appication, then it must be a SWING based App? if yes, look into Swing components JFRAME, etc to load URL into it directly. – usman Apr 01 '13 at 10:41
  • yes, you are right. it is java application not web application all i am trying to do is insert bar chart image which was created and stored in temporary directory, using the url( ) i am inserting it into my jsp page. – Rajesh Acharya Apr 01 '13 at 10:46
  • hello friends...now once it calls jsp page, it is opening in a dreamviewer but i do not want it to be opened there. i want it to be opened in eclipse itself(default browser) wat can i do. ? – Rajesh Acharya Apr 02 '13 at 06:05