I am developing a shopping cart using servlets, have two servlets :
1. ShopingCart.java
2. TotalAmount.java
In ShopingCart.java i have created sessions and synchronise them using
synchronized(session) // lock session protect this from multiple threads
{
TotalAmount cart = (TotalAmount)session.getAttribute("Cart");
if(cart == null) // new sesssion, just create a cart
{
cart = new TotalAmount();
session.setAttribute("Cart", cart);
}
// I have to call cart.display();
and my display method in TotalAmount.java contains (request,response) as parameters.
so, how can i pass the request and response to display method?
yes, i need request and response parameters in display method to save some variable data in session in TotalAmount.java
Please Help..
I can pass the request and response to display method of TotalAmount.java by calling
Display(request,response);
As I already have the object of TotalAmount in ShopingCart.java
As per Java EE specification, servlet are independents and the only official way to interact with one is via their service(ServletRequest req, ServletResponse res) or doXXX method.
If you want to call a specific method of another servlet you have 2 broad solutions :
make the objects know each other by dependancy injection (Java EE CDI, Spring framework, etc.) - clean and neat provided you have one DI mechanism
manually register them via static method - say you want to access Servlet2 from Servlet1
class Servlet1 implement HttpServlet {
private static Servlet2 servlet2 = null;
public static void setServlet2(Servlet2 servlet2) {
this.servlet2 = servlet2;
}
// ... other code
servlet2.display(...);
// ...
}
class Servlet2 implements HttpServlet {
#Override
public void init(ServletConfig config) {
Servlet1.setServlet2(this);
// ... other init code eventually
}
// ...
}
It should work but is not very nice because of the static misuse.
forward to the other servlet and pass a request attribute to indicate that a special action is requested (assuming Servlet2 is named "Servlet2" in web.xml)
in Servlet1 :
request.setAttribute("DISPLAY", true);
request.getServletContext().getNamedDispatcher("Servlet2").forward(req, resp);
in Servlet2 :
public void service(ServletRequest req, ServletResponse resp) {
if (req.getAttribute("DISPLAY") != null) {
display(req, resp);
}
else {
super.service(req, resp); // or directly your code
}
}
Still a nice solution because attribute will not be set in a direct call (of course, you can use req.getRequestDispatcher("/Servlet2URL") instead of getServletContext().getNamedDispatcher("Servlet2"))
Related
Currently , I am implementing the hreflang meta tag in my site and I am facing a problem , I need to create a method that must be executed in every page (in order to load the hreflangs into the model and pass them to the JSP) of my site and this method must have this parameters HttpServletRequest request, HttpServletResponse response.
I tried with this :
#RequestMapping(value = {"/*"})
public class GLobalPageController {
#ModelAttribute
public void setupHreflangs(Model model, HttpServletRequest request, final HttpServletResponse response) {
//code
}
}
But its not working , any idea about how can I achieve this ?
I've done my java web app in Java EE with HttpServlet and JSP. I normally map my Servlet like this:
#WebServlet(urlPatterns = "/main")
public class MainServlet extends HttpServlet{
I do my servlet stuff and would like to pass data to JSP file like this:
RequestDispatcher dispatcher = req.getRequestDispatcher("/main.jsp");
dispatcher.forward(req, resp);
The main.jsp is in my web app folder (I use IntelliJ Idea).
The question is, I've initially made my UI with Vaadin 8. Using following:
#Theme("mytheme")
#CDIUI("users")
#SuppressWarnings("serial")
public class Vaadin extends UI
and then override init.
Now I would like to add a single HttpServlet and override doGet and then call the dispatcher to forward data to jsp. Here's the problem adding Vaadin somehow broke the path to tsp, as jsp does not display, instead a standard vaadin
Request was not handled by any registered handler.
appears, I know the servlet was mapped properly as the servlet starts and does work, what does not work is the running the JSP file.
Can anyone advise?
It seems that you need to implement yet a WebFilter to process JSP. Your dispatcher forwards the request but it is then a request that is again handled by some filter by Vaadin I guess. Also I am not sure if you need any servlet and/or dispatcher (not sure what your actual use is).
Anyway, with WebFilter it is possible to intercept this processing. Check the following example
#WebFilter(filterName="jspFilter", urlPatterns="*")
public class JspFilter implements Filter {
#Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
try {
HttpServletRequest hreq = (HttpServletRequest)request;
String path = hreq.getPathTranslated();
if(path.toLowerCase().endsWith(".jsp")) {
try(Writer writer = response.getWriter();
Reader reader = new FileReader(path) ) {
processJsp(reader, writer);
return;
} catch (Exception e) { /* TODO: handle exception */};
}
} catch (Exception e) { /* TODO: handle exception */};
chain.doFilter(request, response); // forward to filter chain by default
}
#Override public void destroy() {}
#Override public void init(FilterConfig filterConfig)
throws ServletException {}
}
This filter checks all request. If URI (here checked from translated/absolute path) is ending with .jsp it is processed with processJsp(reader, writer) that you might want to implement to do the forwarding to JSP parser or so.
I am using servlet to get request from frontend.
Am i able to make single servlet which could do multiple operation based on url pattern?
Here will be my url mapping
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
<url-pattern>/HelloServletOne</url-pattern>
<url-pattern>/HelloServletTwo</url-pattern>
</servlet-mapping>
That means if i hit to the url as framed below it should invoke its own functionalities.
URL:/HelloServlet: it should do function 1
URL:/HelloServletOne: it should do function 2
URL:/HelloServletTwo: it should do function 3 etc.
How can i achive this by extending servlet.?
Code/link examples are much appreciated.
Regarding your url-pattern you need to know what URL was called. Because a request can be made due to different http-methods (GET, POST etc.) you can use parts of the FrontController Pattern
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet {
private static final String SERLVET = "HelloServlet";
private static final String SERLVET_ONE = "HelloServletOne";
private static final String SERLVET_TWO = "HelloServletTwo";
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
processRequest(req, resp);
}
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
processRequest(req, resp);
}
private void processRequest(HttpServletRequest req, HttpServletResponse resp) {
String path = req.getServletPath();
switch (path) {
case SERLVET:
// ... call your function1
break;
case SERLVET_ONE:
// ... call your function2
break;
case SERLVET_TWO:
// ... call your function3
break;
default:
break;
}
// do something else
}
}
The getServletPath method may only work for explicit url-patterns like you have given. For other information on the URL check this link
You can handle multiple requests by the same servlet by making a contract to have a request parameter like 'ACTION'. Then in your forms add this as hidden field with values like 'ACTION_1' and 'ACTION_2' and 'ACTION_3'. So, in doPost() you can check this parameter value and can invoke respective handling methods in same servlet.
class YourServlet extends HttpServlet{
public void doPost(HttpReq req, HttpResp resp){
String action = reg.getParameter('ACTION');
if('ACTION_1'.equals(action)){
doAction_1();
}
if('ACTION_2'.equals(action)){
doAction_2()
}
if('ACTION_3'.equals(action)){
doAction_3()
}
else {
defaultAction();
}
}
}
I made into.
HttpServletRequest.getRequestURI() returns the URL pattern including /* with query parameter if exist, and HttpServletRequest.getPathInfo() returns the part matched by /* (or null for exact match).
Here in my case i need getPathInfo() where it will returns
HelloServlet,HelloServletOne or HelloServletTwo based on request.
Thanks.
You should not use three different Servlet for this purpose. You should use different methods of Servlet to achieve this.
Use doGet method for get data.
Use doPost method for insert data.
Use doPut method for update data.
Use doDelete method for delete data.
Please refer servlet api documentation for more details.
EDIT:
Read more about this here.
It says the url mapping you have provided must work if you are working with servlet api version 2.5 or greater.
Also, please make sure that you have provided fully qualified name of servlet class in <servlet-name>.
I have a requirement where my requests to pages are to be filtered by an intercepting filter. Based on different conditions, the request has to be redirected to different pages.
I have created a bundle and registered the servlet and filter in the bundle activator, taking a cue from here.
Following is the code snippet:
Hashtable initParams = new Hashtable(); // to pass a dictionary object to
// service.register
initParams.put("sling.servlet.resourceTypes","/login");
initParams.put("sling.servlet.extensions","jsp");
service.registerServlet("/myServlet", this.myServlet, initParams, null);
initParams = new Hashtable();
initParams.put("sling.filter.scope","REQUEST");
service.registerFilter(this.checkPageRequest, "/.*",null, 2, null);
service.registerFilter(this.checkValidSession, "/.*", null, 1, null);
I am facing two issues:
I am not able to identify where I am mapping a filter to corresponding servlet. My understanding is that the checkPageRequest filter would be called followed by checkValidSession. if there is no requestdispatcher.forward(..), the myServlet servlet would be hit. In my case, the filters are getting called as expected by using filterChain.doFilter(..), but the servlet is not getting called.
Since my filters have to intercept all the page requests, so when I do a requestdispatcher.forward(..) to any page, the same filter gets called again, getting into a loop.
I am developing this as OSGi bundle to be deployed on a DayCQ environment.
As you're working with Sling, you should rather use Sling's standard SCR-based methods to register your components, described at http://sling.apache.org/site/filters.html for filters and http://sling.apache.org/site/servlets.html for servlets.
Registering servlets and filters directly with the HttpService as you seem to be doing will "fight" with the SlingMainServlet, which should get all requests in a Sling application.
Well your Filter and Servlet should have the same URL mapping. Ofcourse the Filter will get called first.
Let the Filter preprocess the data and let the servlet add the data in request object and dispatch it to JSP. See below Example,
#SlingFilter(order=1)
#Properties({
#Property(name="service.pid", value="com.videojet.hiresite.filters.AddNewUserFilter",propertyPrivate=false),
#Property(name="service.description",value="Preproceesing Filter", propertyPrivate=false),
#Property(name="service.vendor",value="XXX Tech", propertyPrivate=false),
#Property(name="pattern",value="/services/videojet/v1/.*", propertyPrivate=false)
// The Property Above property Maps your Filter to URL
})
public class AddNewUserFilter implements javax.servlet.Filter{
private final Logger log = LoggerFactory.getLogger(this.getClass());
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
log.info("AddNewUserFilter Invoked Invoked");
// Preprocessing here and just call chain.doFilter(xxx);
And the Servlet
#SlingServlet(
paths={"/services/videojet/v1/AddNewUserController/view","/services/videojet/v1/AddNewUserController/userExists"
,"/services/videojet/v1/AddNewUserController/addUser"}
)
#Properties({
#Property(name="service.pid", value="com.videojet.hiresite.controllers.AddNewUserController",propertyPrivate=false),
#Property(name="service.description",value="Adds new user", propertyPrivate=false),
#Property(name="service.vendor",value="XXX Tech", propertyPrivate=false)
})
public class AddNewUserController extends SlingAllMethodsServlet{
/**
* #author Oliver Kaunds
*/
///content/dam/videojetdocuments
private static final long serialVersionUID = 1L;
private final Logger log = LoggerFactory.getLogger(this.getClass());
#Reference
protected AddNewUserService addNewUserService;
#Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException
{
log.info(":: Do GET Called ");
String path =request.getRequestURI();
log.info("PATH :: "+ path);
try{
if(path.equals("/services/videojet/v1/AddNewUserController/view"))
{
/* Do the dispatching here One Servlet can have more than one Request
Mappings . My Each mapping serves a purpose. */
List<HRRepresentative> list =addNewUserService.getHRRepresentative();
request.setAttribute("hrList",list );
HttpSession session = request.getSession();
String userOperation =(String)session.getAttribute("userOp");
request.setAttribute("userOp", userOperation);
session.removeAttribute("userOp");
//throw new Exception("My Exception");
RequestDispatcher dispatcher =request.getRequestDispatcher("/content/videojet/en/addnewuser.html"); // Dispatch to JSP here
dispatcher.forward(request, response);
}
This is a Tried and Tested code
I've got this issue, recently I read about the REST arquitecture and it makes a perfect sense, so I'd like to achieve a RESTful web application.
Now, I'm following the Front Controller pattern that means that all of the URL mappings go to the controller.java servlet, I map the by specific URLs, not by using the /* wildcard,
the controller implements the four HTTP methods POST,GET,PUT,DELETE, each method calls the controllers service method and there I determine based on the HttpServletRequest and pathInfo the action to execute.
Controller.java
#Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
IAction action;
View view;
try {
action = ActionFactory.produceAction(req);
view = action.execute(req, resp);
switch (view.getDispatchMethod()) {
case REDIRECT:
resp.sendRedirect(resp.encodeURL(view.getResource()));
break;
case FORWARD:
req.getRequestDispatcher(view.getResource()).forward(req, resp);
break;
case INCLUDE:
req.getRequestDispatcher(view.getResource()).include(req,resp);
break;
default:
}
} catch (ActionFailedException uae) {
req.setAttribute("ActionName", "Action");
req.setAttribute("FailCause", uae.getMessage());
req.getRequestDispatcher(VIEW_FAIL.getResource()).forward(req, resp);
}
}
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.service(req, resp);
}
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.service(req, resp);
}
#Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.service(req, resp);
}
#Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.service(req, resp);
}
I've run into a particular issue when loading a specific order by the URI /orders/*, it is mapped to the controller servlet, the the action is executed and I load the appropriate order the action returns a View.java class
//ommited accessors and mutators for brevety.
public class View {
public enum DispatchMethod {
INCLUDE, FORWARD, REDIRECT
}
private DispatchMethod dispatchMethod;
private String resource;
public View(DispatchMethod dispatchMethod, String resource) {
this.dispatchMethod = dispatchMethod;
this.resource = resource;
}
}
Then the request is dispatched according to the getDispatchMethod() of the returned view.
Now, here is where the loop gets triggered, I use the following URL, myapp/orders/78965 /orders/* gets mapped to controller.java the appropriate action is executed and the correct order is found by the pathInfo() the returned view is new View(View.DispatchMethod.FORWARD,"order_details.jsp") the problem is that with the three available dispatch methods REDIRECT,FORWARD and INCLUDE a request is re-triggered on the URL and so on and on and on I never reach the order_details.jsp that renders the data.
So, how would you avoid the looping, as I'd like to preserve the URI displaying the order number I use the forward method, also, I'd like to do it using servlets, I've heard of the UrlRewriteFilter maybe in the future, but right now, how would it be done using "Plain Vanilla" since I'm using the Front Controller pattern, will it be necessary to add an additional servlet in the /orders/ URI ?
Any help or insights is truly appreciated.
EDIT 1:
Pasted the source code of the controller, a very basic one, I have my suspicions that the way the service method calls all of the overriden do[Method] of the servlet is triggering the loop and that it may be solved by splittig them.
Implementing a RESTful HTTP interface in Java is a lot easier using a JAX-RS implementation like RESTEasy or Jersey.
Using a Front Controller to dispatch requests to the right resource is a good approach, it's exactly the approach taken by these JAX-RS frameworks. I fear you may be re-inventing the wheel here by writing a bespoke URL parsing and dispatching mechanism when this can be taken off-the-shelf.
JAX-RS is a lightweight way to expose resources. By using a couple of simple annotations you can expose a REST interface without any plumbing required. For example:
public class Order {
#GET
#Path("/orders/{orderId}")
#Produces("text/html")
public void getOrder(#Context HttpServletResponse response,
#Context HttpServletRequest request,
#PathParam("orderId") String orderId) throws ServletException, IOException {
// ... create view and add to request here
request.getRequestDispatcher("orders.jsp").forward(request, response);
}
}
You can see how simple it is to attach this class to a URL path (using the #Path annotation), and how easily you can parse values from the URL using #PathParam. Since you get all the plumbing/dispatching/parsing off-the-shelf, you can concentrate on the bits of your app that are specific to your domain (such as what an order contains).