wicket url parameters with stateful pages - java

on an application i develop i'm trying to keep a nice and readable url for the application's pages.
i start with url as follows:
http://somedomain.com/context/?param1=value&param2=value
where context is the application mount path and param1 and param2 are some parameters passed to the application.
the problem is that when i move to another page the url changes to be as follows:
https://somedomain.com/?wicket:bookmarkablePage=:something&Title=something&group=something
the way i'm moving between pages is as follows:
getRequestCycle().setResponsePage(new otherPage(obj1, obj2,
pageParameters)
where obj1 and obj2 are objects required for the initialization of the page.
as i understood from this post using
RequestCycle().setResponsePage(Page page)
creates a stateful page which is not bookmarkable and does not display the parameters, while using
RequestCycle().setResponsePage(Class<C> pageClass, PageParameters
pageParameters)
creates a stateless page which displays the parameters.
the problem is that i use the first one, as i must create the page myself so i could pass the two objects to it.
is there any way to keep the url in its original bookmarkable format and remove wicket's parameters from it?
i tried almost every suggestion posted on this site and others but none of them fits my case, as they suggest to let wicket create the page itself (with the second option of setResponsePage).
any help will be much appreciated.
thanks

from your url example i see you are using Wicket 1.4. I believe 1.5 will do what you want out of the box. in Wicket 1.4 you can achieve a similar result by mounting the page using HybridUrlCodingStrategy.

Related

Create new static address dynamically in Java web application

When developing a Web Application in Java, launching in Tomcat, I need to be able to create (dynamically) a new static address (link,URL) in the server that will be used to view the information of a new item, let's call it new_item_001, which have been just created by one user.
Say I want to create a new address
www.domain.com/webapp/items/new_item_001
which can be used to render a view of the contents of new_item_001.
Which is the best approach to do this?
Should I dynamically create a new servlet class for this view?
Should I dynamically create the folder items and one html file new_item_001 for this item inside of it?
Should I edit the server address mapping rules to create this static address and map it to a central servlet which somehow knows which item to display?
I understand the question is ill posed, and that I am far from even understanding the issue, so I would like some guidelines on what to look for.
None of the above.
You should simply have a servlet mapped to /items/*. When a request come to this servlet, analyze the actual path of the request, extract the part after /items/ to know the actual value (new_item_001) in your example, get the data corresponding to this item from the database, and send it to the browser.
Using a true MVC framework like Spring MVC would make that much easier. You could simply map a method of a controller using
#RequestMapping("/items/{itemId}")
public Item getItem(#PathVariable("itemId") String itemId) {
...
}
and let the framework do all the URL parsing for you.
I would like to tackle this in a simple way. Creating a servlet for each created item would be overkill and become quite cumbersome to manage after a successful run of the application for some time.
Changing/editing server mapping URL looks very naive approach and is not scaling too. Let configuration be there and change them only when you actually need to change them.
My suggestion is to create one servlet that handles all these requests. For example, you may save item information on a datastore or on file system(i.e images uploaded by user etc..). Next time a GET request is received by the application to fetch saved information of an item, servlet should be able to reference the item on database associated with the item id on the URL. If you don't wish to expose item id/surrogate key in the database, you can also have a simple mapping between them by implementing your own logic. Frameworks like Spring MVC do a good job in mapping URLs to resources like this should you wish to use a framework.
Additionally to minimize the number of requests to the same item, you can also implement an HTTP caching strategy(i.e. ETAG, If-Modified-Since) by instructing your web server at the time of first GET request from a user.

In BrightSpotCMS HOWTO inspect the "request" object from HttpServlet before the JSP is rendered for a "Content" object

In many MVC frameworks when a request is made it goes to a controller/action class (based on the URL pattern among other things). If the developer wants to do something with the request object or other processes then it does that with in the execute or doGet or doPost etc methods & then forwards it to the dispatcher. The response type could be a JSON, JSP, XML etc.
I have a brightspot cms webapp in which I want to do something similar. It is based on the open source project dari framework.
In case of a object of type Content if I want to setup some pre-processing of variables to be used in the JSP page based on the request object, how can I do it? I am unable to find the point of intervention between the request going to conten type object AND request being forwarded to the backing JSP page.
I know I can just add scriptlets to the JSP page, but I had rather not do it for variety of reasons.
I was able to resolve this by adding a Filter for the URL pattern I was interested in. More info here.

Passing variables from JSP to servlet

All the time, when I searched on Google, I got dozen of answers which are posted in Stackoverflow about passing variables to servlet from JSP.
But I am wondering, I don't get answer of: How to pass a variable from JSP to a servlet class? Is it possible?
Actually I am doing a simple PhoneBook application. Here I have to send contact id to a servlet for editing and deleting. How can I pass this value?
I know, we can pass variable from servlet to JSP by using request.setAttribute(key, value)
But when I used it to set variable in JSP and again get it by using session.getAttribute(key ) then result is null.
God help me.
The standard way of passing/submitting data to the server in the pure Servlets/JSP world (as in your case from the JSP to the servlet) is by using HTML form, i.e. the same way as when using other technologies (ASP.NET, PHP etc). And it doesn't matter whether it is a pure HTML page or JSP page. The recommended/most used method of submitting data from the form to the server is POST.
You also can pass data in the query string that is contained in the request URL after the path (this also happens when instead of POST you use GET method in the form). But that is for the simple cases, like constructing URLs for the pagination etc (you can see the example of constructing URLs with the additional queries here: Composing URL in JSP)
Example of passing parameters in the URL:
http://example.com/foo?param1=bar&page=100
For the difference between submitting data using GET and POST methods read here:
GET versus POST Requests on HTML
Forms
In HTML forms, what’s the difference between using the GET method
versus
POST?
So you can configure some servlet to process data sent/submitted from a JSP or HTML etc.
It is highly recommended to submit data using POST method and respectively to process the submitted data using the doPost() method in your servlet.
You will then get the parameters passed by the client in the request by using one of the following ServletRequest methods:
java.lang.String getParameter(java.lang.String
name)
java.util.Map
getParameterMap()
java.util.Enumeration
getParameterNames()
java.lang.String[] getParameterValues(java.lang.String
name)
Here is a nice tutorial with examples: Handling the Client Request: Form Data
The above tutorial is from the following course:
Building Web Apps in Java:
Beginning & Intermediate Servlet & JSP Tutorials
Another way of exchanging data using Java EE is by storing data as attributes in different scopes. (Following is the excerpt from one of my answers on SO)
There are 4 scopes in Java EE 5 (see The Java EE 5 Tutorial: Using Scope Objects). In Java EE 6 and in Java EE 7 there are 5 scopes (see The Java EE 6 Tutorial: Using Scopes and The Java EE 7 Tutorial: Using Scopes). The most used are:
Request scope
Session scope
Application scope (Web Context)
You can store some data in all the above scopes by setting the appropriate attribute.
Here is a quote from the Java EE API docs related to ServletRequest.setAttribute(String, Object) method in regard to request scope:
void setAttribute(java.lang.String name,
java.lang.Object o)
Stores an attribute in this request. Attributes are reset between
requests. This method is most often used in conjunction with
RequestDispatcher.
...
So with every new request the previous attributes you have set in request will be lost. After you have set an attribute in a request, you must forward the request to the desired page. If you redirect, this will be a totally NEW request, thus the attributes previously set will be lost. (If you still want use redirection read this: Servlet Redirection to same page with error message)
Those attributes that are set in a HttpSession (in the session scope) will live as long as the session lives and, of course, will be available to only the user to which session belongs.
As for the context attributes they are meant to be available to the whole web application (application scope) and for ALL users, plus they live as long as the web application lives.
Also maybe this article will be useful for you as well: How Java EE 6 Scopes Affect User Interactions
Also pay attention to the following issue. You wrote (quote):
I know , We can pass variable from servlet to jsp by using request.setAttribute(key , value)
But when I used it to set variable in jsp and again get it by using session.getAttribute(key ) then result is null.
As the users #neel and #Sanchit have noticed, you are setting an attribute in the request object, but trying to get it back from the session. No wonder you are getting null in this case.
Hope this will help you.

what is a good pattern for serving a web page given a HttpServletRequest?

I've created a abstract base class Page that figures out how to construct a dynamic web page. I'm trying to come up with a good way to generate a Page based off the GET request that comes in as a HttpServletRequest. For example...
public class RootServlet extends HttpServlet {
#Override
public void doGet(HttpServletRequest request, HttpServletResponse response) {
Page page = Page.generatePage(request);
// do stuff with page and write back response
}
}
In the generatePage() method, I somehow have to figure out what page is being requested, build the correct page, and then return an instance of it. But I'm not sure how to do this well... for example, I need to handle these kinds of URLs coming in:
http://example.com/ : build the default home page
http://example.com/ab123 : build the page corresponding to the given token "ab123"
http://example.com/about/ : build the "about" page
http://help.example.com/ : build the "help" page
Each of these "pages" extend the abstract base class Page so they know how to build themselves, but I'm not sure how to determine that the AboutPage needs to be built, or the HelpPage, as opposed to the default HomePage.
I'm using Apache Velocity as the template engine, so these Page objects really contain only the important information needed to generate that page, like which styles and scripts to use, and the relevant content to be displayed on the page.
I would think there are better ways to do this than to look at the end of the URL and see if "about" is a substring to build the AboutPage, for example. Any suggestions?
There are dozens of off the shelf tools frameworks that do this for you. In the very least I'd suggest Spring MVC which will work with velocity.
Spring MVC has a great way to deal with this kind of stuff using controllers with annotated methods to handle the specific pattern that you want.
They have a great example application here:
https://github.com/SpringSource/spring-mvc-showcase
Anyway, it is not a good practice to build your pages using java code.

JSP and Java Servlet problems

I am new to JSP and Java Servlet. I am quite confusing about Session object. I saw session When I learned PHP session and cookie. Are there complete different things? And how a Session object is created, structured and used. This object is in JSP or Java Servlet? could somebody tell me this by words(like concept). In addition, in what situation a JSP page would be appropriate for?(when should I use a Java Servlet and when should I use a Java Servlet Page).For Java Servlet object, for example, ran an email site. There will be a lot of users. How does one Java Servlet object deal with interactions from so many browsers?(like hundreds of logging, reading, etc.)I know there should be only one copy of Java Servlet object exists. But why? If only one there, when is it created and destroyed. Ah... So many questions. If someone can help me I will really grateful for this. Thanks a million!
? And how a Session object is created, structured and used.
It depends on the implementation of it, here is the contract
This object is in JSP or Java Servlet?
This is as an implicit object in jsp and it can be retrieved from request instance from servlet's service method
what situation a JSP page would be appropriate for?(when should I use a Java Servlet and when should I use a Java Servlet Page).For Java Servlet object, for example, ran an email site. There will be a lot of users. How does one Java Servlet object deal with interactions from so many browsers
Use jsp as view servlet as controller, See MVC
now there should be only one copy of Java Servlet object exists. But why? If only one there, when is it created and destroyed.
each request is served in different thread so why to create different instance, we can have one instance of servlet doing all this for us. and its alive until garbage collection clears it
See : Head First
You can think about a session object as a file . every user have a session with id called jsessionid , the structure of a session is normally a map data structure which store key value
in Servlert you can construct a session object like this
HttpSession session = request.getSession(true);
then you can add item to the session like this
session.setAttribute(string ,object); ex : session.setAttribute("username","foo");
the session object exist in servlet and jsp , and btw jsp eventually is a servlet
but the difference is that when u want to use session in a jsp page there is no need to construct it. its defeind by default just use it
session.setAttribute(string,object);
JSP page used when a page contains a lot of html element and have a lot of design
and jsp let you maintain the page easily on the other hand you can use servlet as jsp page
but you will deal with every line o html source code
JSP is preferred as a view in the MVC model
and the servlet as controller .
the server keeps one object for each servlet and when a new request come the servlet object put the new request (client ) in a new thread so if you have 100 client at a time the is
100 thread in server . but you can configure the server to construct more than one object of a servlet .
i hope i could help u ..
I think many of your questions would be answered if you had a look at the Java Servlet Life Cycle.

Categories

Resources