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
.
You can pass objects to the bean using
<jsp:setProperty
and EL expressions. You have to modify the bean for getter and setter methods.then use in the JSP
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
You would be able to use some servlet class. for instance :
you must add into the web.xml following:
"/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).