1

How to pass JSP implicit objects like (request, response) in Java. I want to access JSP Implicit Objects in Java code. Please suggest how to achieve this?

My Java code is:

package test.here;
import java.sql.*;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ibm.commerce.server.WcsApp;
import com.ibm.commerce.server.JSPHelper;

public class SomeBean {
  String punchOutRes = null;
  HttpServletResponse response;
  HttpServletRequest request; 

  public String getPunchOutRes() {         
    response.setContentType("text/xml");
    return "testing";      
  } 
}

Here when I am trying to set or get anything in request and response, I get NUllPointerException.

Roman C
  • 49,761
  • 33
  • 66
  • 176
user1602657
  • 141
  • 2
  • 6
  • 8
  • 1
    why u want to have access to these objects in java beans and anyway you already have access to implicit objects in servlets. you can write your business logic in java beans and access that bean in servlet. – Sandeep Kumar Aug 16 '12 at 09:35
  • But I donot want to access in Servlet. Instead in a plain java class. – user1602657 Aug 16 '12 at 09:37
  • 1
    If you want to access the implicit objects of JSP, you can access via servlet and pass it on to your plain java methods as parameter. But I would not suggest this method as you are passing the bigger object for small things. – Kalpak Aug 16 '12 at 11:59

3 Answers3

1

You can pass objects to the bean using <jsp:setProperty and EL expressions. You have to modify the bean for getter and setter methods.

public class SomeBean {
  String punchOutRes = null;
  HttpServletResponse response;
  HttpServletRequest request;

  public HttpServletResponse getResponse() {
    return response;
  }

  public void setResponse(HttpServletResponse response) {
    this.response = response;
    try {
      response.getWriter().println("Some Output");
    } catch (IOException e) {
      e.printStackTrace();  
    }
  }

  public HttpServletRequest getRequest() {
    return request;
  }

  public void setRequest(HttpServletRequest request) {
    this.request = request;
  }

  public String getPunchOutRes() {
    response.setContentType("text/xml");
    return "testing";
  }
}

then use in the JSP

<jsp:useBean id="someBean" class="beans.SomeBean" scope="request"/>
<jsp:setProperty name="someBean" property="request" value="${pageContext.request}"/>
<jsp:setProperty name="someBean" property="response" value="${pageContext.response}"/>
Roman C
  • 49,761
  • 33
  • 66
  • 176
0

IMO I don't think it is possible to pass implicit objects directly to java class so but one thing you can do is pass session and request object from servlet to a java class either in some method or in constructor of java class.

Also see this question

Community
  • 1
  • 1
Sandeep Kumar
  • 13,799
  • 21
  • 74
  • 110
0

You would be able to use some servlet class. for instance :

public class ActionServlet extends HttpServlet
{

public ActionServlet()
{ 
  super();
}

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
{
    request.setCharacterEncoding("UTF-8");      
    String action = request.getParameter("action");        

    //do smth. with "action" you are able to use your class "SomeBean"

    response.setContentType("text/plain");
    response.setCharacterEncoding("UTF-8");
    response.sendRedirect(someUrl);                

}
}

you must add into the web.xml following:

 <servlet>
    <description></description>
    <display-name>ActionServlet</display-name>
    <servlet-name>ActionServlet</servlet-name>
    <servlet-class>com.your_package.servlet.ActionServlet</servlet-class>
 </servlet>
 <servlet-mapping>
    <servlet-name>ActionServlet</servlet-name>
    <url-pattern>/Action</url-pattern>
 </servlet-mapping>

"/Action" is your url.

I gave a simple example, I didn't take account the either GET or POST method there. Also I would advise to use JSTL. Using some scriplets in the "view" is bad style of codding:)

However, it's better to use MVC pattern(Spring/Struts).