Implementing Single User Per Page concept in J2EE web application - java

Are there patterns for implementing a single user per page model in a J2EE environment?
By single user per page I mean, if I access:
mysite.com/foo/pageA
If someone else tries to access this page, they should not be able to until I:
-navigate away from the page
-close the browser
As it stands now, I have implemented this feature using a map that is a member variable in the servlet that handles requests for foo/*. The keys for the map are page names (enum), and the values for the maps are a boolean value that represents whether or not that particular page is locked.
Before the page is served, I look at the map value to determine if the page is occuiped, and if it is not, I send the user to the page and update the map entry (synchronizing on the map), if it is , I send the user to a page displaying the message that the page is occupied.
To implement the locking, I use the onbeforeunload javascript method, in which I use an ajax post call to update the map entry to false, again, synchronizing on the map.
I know that if the application expands to more than one instance, the locking/unlocking will have to be persisted, but as it stands now, the application only serves about 10 users.
It seems to be working, but I was wondering if there were established patterns for accomplishing this, or if the idea of locking a page to only allow a single user is bad practice in a web application.

Related

How can I generate and store pages with unique URLs with servlets?

I am just learning about servlets and Tomcat. I am trying to make a simple cart application that presents users with a list of things to buy then submit their order to the server, which will generate and store a web page on the server with a unique url that shows what they ordered.
I don't really know where to begin on this, as I have no idea how to generate the unique web page. I can only make a servlet named show-order which will cause the page to redirect to a static url (i.e localhost/cart/show-order) that will change every time a new order is placed. I would like to be able to view all past orders via unique URLs.
I have read about a method called encodeURL() which will return a unique URL if cookies are disabled, but I don't care about cookies, I want to always return unique URLs for the orders...
So to summarize: I can't figure out how to write a servlet which will produce multiple unique web pages and store them on the server for future viewing.

How to retain form values in Struts2?

I have an action say actionA associated with a JSP page say A.jsp and another actionB associated with B.jsp. There's a form on A.jsp. User submits some data which is set in actionA properties. Now user navigates to B.jsp (or any other page for that matter) and then comes back again to A.jsp. The form data in the fields of A.jsp is lost when the user comes back. I want to retain that data.
I cannot use hidden fields since I have a lot of fields and cannot use sessions either. Is there a way in Struts2? If not can you suggest me any other way?
When you want to send data from a pagexto another, as you're working on a http protocol you have two ways, use parameters (hidden fields in post submit or url parameters in get) or you have to use session.
If you can't do neither, think about saving your A state on a database and pass only a key that let you retrieve the A state when you are back.
Sorry for the generic answer but it's all I can say if you don't give more details.

Alternative for session bean

I have web application based on jsp and spring mvc where i need resolve this task :
The user must be able to add new instances of the main entity using wizard dialog. The wizard consists of 3 steps:
On the first step there must be a form which allows filling main entity’s fields, including association with the entity related as many-to one (it’s recommended to use drop-down field). The form should contain fields of different types: text, number, date, radio button, etc. Some fields should be required and some are not.
Example: input name, surname, birth date, phone, number of kids, select gender (radiobutton), department (drop-down), etc.
On the second step user fills additional attributes, including association with the entity related as many-to-many with the current one.
Example: associate employee with skills that (s)he has (checkboxes), add some note (textarea).
On the third step all the fields from previous 2 steps should be displayed as read-only fields. The user should confirm saving this data into database. After the user confirms saving, the data should be saved into database, and user should be redirected to the page with the list of objects.
How can i transfer and hold information without using sessions(Http session, session scope)?
You need to keep state across multiple server interactions. There are several possibilities, in general factors such as the size of the state data to be retained influence our decisions.
It sounds like you have some small number of hundreds of bytes here, so you're not particularly constrained by size - a few Megabytes would be more of a challenge.
First possibility, keep it all in the browser in JavaScript variables, no actual need to send anything to server. This is typical of a modern dynamic Web UI, where the server serves up data rather than pages. Sounds like you're in a multi-page world so discount this option.
Second, just put some data (possibly encrypted, in a cookie) effectively the browser is keeping the data for you, but it's shared across the pages.
Third use Http Session state - you case does sound very much like a typical candidate for a session. Why do you want to avoid it? Depending upon your server's capabilities this approach may not give great resilience behaviour (if the state is on one server instance then all requests for a session must be served by the same server). Note that HTTP Session and EJB Session Beans are not the same thing, HttpSessions are lighter weight.
Use a custom session "database" - maybe literally a SQL database maybe something lighter. For larger scale data entry cases, where a user may take 10s of minutes to complete many pages this may be the best option - the user's work is saved should they need to break off and resume later. It's more development work and you need to look at housekeeping too, but it's sometimes the best option.
In summary: be very clear why you reject the "obvious" HTTP session technique, in terms of simplicity it's where I'd start.

Retaining previous form data

I have a form that I could put on to one page, but for aesthetic reasons, I want to split into two. The original form, on submission, would go to a Servlet which would get the form data and insert it into a database. However, I don't know how to make this work when it is split into two different forms on two different pages. My forms are currently in html but I could change them to JSP if that is the solution. I do not want to use hidden fields; if there is no way to do it without hidden fields, then I will just put it on one page as my form has quite a few fields and hidden fields would mean basically doubling the amount of code. I also only want to use html or jsp for the forms; I don't want to use JavaBeans, and I want to avoid scripting in the jsp's. I have already done this by simply dividing the database logic into two and using the ID of the last inserted object as a hidden field, and the second form then uses that ID to update that item, but this is not an elegant solution and could cause a problem if one user submitted the first form and a different user submitted the second (the wrong item would be updated). Is this possible?
Well, this is one of the things the Session can be used for: store the data from the first page in the HttpSession and then in the second page retrieve the session data and save it to the database.
You can use javascript pagination (It'll save page loading time. Many online test applications adopt this approach)
a) Either on submission on each part of form data will send to server using ajax call.(partial submission)
b) Or the whole data will be maintained at client side only until whole form is completed and submitted successfully.
(depends on your application need)
You can logically relate each part of form with some unique id & session id combination. In addition, if you are not willing/required to store whole form data in session, you can have primary key in session. It'll make database update process easier for 2nd or next part of form data.

Creating friendly urls to dynamic resources in struts2

I have a struts2 application with a single page that may show one of a number of values stored in a database. The application is for a school with many departments and each department has many programs. The department page is accessed using a url like this
department.action?id=2
and the DepartmentAction will load the Department with id = 2 for display. All this is fine if the user is just browsing around the site but it gets uncomfortable if I want to provide a link to say the Engineering department in the newspapers. The link will have to be www.myschooldomain.com/department.action?id=2. I see a number of problems with this.
First, it is not user friendly. Second, it is prone to be broken because the departments are dynamically maintained and the id for a department could change without warning making the link unstable.
I would prefer to print a url like this: www.myschooldomain.com/department/engineering and have that somehow go to department.action?id=2.
My thoughts so far: create an action that will parse the url for the department name at the end then look it up by name. Maybe I could add a friendlyurl field to the database for each department.
But the question is: Is there a better way to do this in struts2?
Thanks.
Update (May 2009): I just happened to stumble back over this question and thought that I would say what I did to solve it.
I created a new package in the struts.xml called departments. In this package there is only one action mapped to *. So it catches all requests to mydomain.com/departments/anything.html.
In the action class I simply parse the url and look for the part between departments/ and .html and that is the name of the department so I can do a lookup in the database for it. This has been working fine for almost 5 months now and I have implemented it for other areas of the site.
You could use the URL Rewrite filter
This avoids the need for any additional servlet or Java code but requires XML descriptors.
This is normally done by mapping a servlet to, in your case '/department', and then using the path information (e.g., '/engineering') within the servlet to determine the ID.
Since the Struts2 dispatcher doesn't implement this behavior, it might be simplest to write your own servlet. This servlet would be configured with a map of valid "friendly" names to the unfriendly numeric identifiers. This could be an actual Map or it could be done with a database finder method.
The result of getPathInfo() would be used to look up the ID, and the request would be forwarded to the department.action. Handle the null case too, which means the user is trying to browse the /departments/ directory.

Categories

Resources