Forms in jsp, what to use instead of hidden field - java

I'm very new to jsp and is unfortunately working with Websphere 5. I'm now trying to make a simple web page that can show information from the enterprise beans and got instructed to send information from the jsp-page to the servlet via a hidden field in the form. This seems like a sort of out dated way to do things. Are there better ways?
As I said, I'm new to the jsp-part, but do know Java and html, but no JavaScript.
Grateful for help!

so you want
store some data used by jsp and servlet
want it to be hidden to user
No html hidden form element.
No javascript
how about maintain the data in session object?

Related

Calling java code from java script in JSP

I am newbie in UI i.e. front end. I am using Javascript and JSP. I need to perform the below task.
I have a textarea in jsp page which gets populated from a text file which i read using Java. Now, I want to empty the text area and that text file on clicking a button. Emptying the file is not an issue only i don't get how to transfer call from jsp file to Servlet on clicking that button.
Please help.
There are two ways to do this:
the classical way is form submission:
http://www.tutorialspoint.com/jsp/jsp_form_processing.htm
e.g. you embedded a form in your jsp page that will send the data to your server when submitted.
the second way is via ajax
https://en.wikipedia.org/wiki/Ajax_(programming)
I would suggest you google for the basics of form submission (which is not jsp specific) or doing ajax requests with javascript (with a library, for example jquery)
there are two ways one by jsp and another by javascript
if the button is submit type and if you are using a form then
if the button is simple a button then
function x()
{
window.location.replace("servlet name");
}
please tell me if you have any doubts
There are many ways to do so.
use jquery and ajax, its so easy.
similar question here:
jQuery Ajax POST example with PHP
you can find documentation here
http://api.jquery.com/jQuery.ajax/
or use JSNI.
https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI

MVC Architecture issue

I have been reading a lot on MVC but really don't know if i am clear on the concepts of MVC or not
recently developed an application what i did is
1)on jsp load called a function
2)using AJAX called a servlet and servlet is there performing all the logic
3)servlet called a java bean and a java class to perform some logic and return result
4)based on the result returned form the class i am displaying an image say if result is 1 then image A ,if 0 then Image b
5)on servlets POST method i am using out.println()-->to write the complete output
6)the function on jsp after returning the call will set the innetHTML of required div by the output generated by the servlet
now say the output servlet is producing is the table
instance name|instance state
now if i want at some time to change the display for this table to say
instance state|instance name
to do the above mentioned change i have to recompile my servlet and redeploy the war
is it really a MVC?
and someone suggested me to use JSON store object of a bean containing data as JSON and then return the JSON object to the jsp
and at jsp using this object contruct the table!
any pointers on this will be of great help!!
Whether you have to redeploy usually depends on your development environment. If your using an IDE that builds automatically as you make changes, and the server is run from the IDE you are using, you may not need to rebuild the war. You can always try to view the source code to see if you need to redeploy. Back end code usually has to be redeployed.
Based on the ajax reponse obtained.
You can hide or show the images that you tend to.
How about getting the image link instead of out.print printing the bytes[] if i am not wrong.
When you want to redirect to another page, how about redirecting it from the servlet itself using response redirect.
Lets have a quick look at what is MVC?
MVC(Mode-view-controller ) as the name suggests is software architecture pattern , which encourages application to have its Model Classes (i.e domain models / DTOs) views (i.e can be JSP, JSON etc) and controller (i.e Servlet) to be as modularized as possible so that it encourages re-usability, loose-coupling between the different layers and Seperation of Concerns.
So the key idea behind this to encourage Seperation of Concerns .
Say i want to change the view from JSP to freemarker view , if MVC is tighly followed , i should be able to accomplish the change with minimum to no impact to Controller layer (i.e Servlets)
Well you see this can only be achieved if had clear layer separation in my webapp.
If i had just scattered all the functions without regard to MVC like having views generated from the Servlet, or making service level calls like accessing the DB directly from the Controller etc is bad because any change in the view or the Database layer will cause massive changes at the Servlet .
So to answer your question , your servlet should not directly produce the HTML output.
Store all the objects that would want to generate view in Request Attribute and access it in JSP
And to recompile Sevlet doesnt mean you dont follow MVC , just that by following MVC your changes are minimal and are grouped at a single place.
For now drop JSON concept , make it plain and simple
Go through this tutorial , which fairly explain you how to achieve a neat MVC
Jsp MVC tutorial.
Once you grasp , you can always add more complex things like JSON, AJAX , Asynchronous Request etc

Easy way for AXAX refresh JSP or Spring MVC page?

This seems a bit like a rudimentary question for java web development but...
How would I go about refreshing data in a JSP page? I mean, I get the basics (use jQuery for AJAX, Spring MVC for the "Controller" & get data requests). What has me stumbling is what is the easiest way to render the updated data to the page (given, the JSP is all server side...which is not conducive to client side updates)?
I've considered:
Using Mozilla Rhino + Velocity in the javascript - this seems a bit cumbersome
Using the "new" Spring AJAX MVC improvements - the examples for this seem a bit confusing to me.
Returning a semi-rendered String in the Spring Controller get method via the business logic+velocity - I'm not sure if it is "correct" to do it this way, it feels a bit dirty to build up the view object in the Controller class.
Is there an easy way to do what I am asking? I basically have an html table that needs to be repopulated on an interval. Surely I'm missing something here.
TIA
My suggestion would be to specify a div for the content you want to refresh.
At the specified intervals, reload the div with fresh content from the server. I would recommend generating the html at the server and just jQuery('').load() the url. But you could also just get json data from the server and create your markup on the fly, but this is problematic with large records.
Hope that helps.
About generating JSON or a partial view in the controller, both options are valid. I'd go with JSON if the HTML to generate/modify isn't too complex, and I'd choose returning a HTML fragment for something like refreshing a big table, or load a new complex panel, etc. To generate JSON I usually go with a Spring MVC controller method with a bean return type annotated with #ResponseBody.

jqGrid using a servlet to store the data, how to access the data from a different java class

I'm using jqGrid for a table input and setting up the url as a servlet which will deal with the GET and POST requests and save the rows to a Java object.
I'm using webwork web framework and I was wondering how I can get access to the object that the servlet is saving the data to.
One way I have thought of is to just call the GET method from the Java action class which the servlet will return a JSON string with the object data.
Is there a better design for doing this?
This is probably not too clear so ask questions so I can help get across my point.
Thanks
Since you are using WebWork, why are you writing a Servlet? The jqGrid can post data to an action directly. Let the action do the work that the servlet is doing.

Clear HTTP Session

I am trying to clear everything on my HTML form when I visit it from a hyperlink entry.
Any ideas? My development language is java.
are you using session-scoped data? if so, close your browser and open it again.
I'm not sure the application is, but one way to accomplish this would be to use JavaScript. For example, if it is acceptable to clear the form every time that page is visited you could write a quick function that clears the form when the page is loaded (i.e., using the onload event).
If you only want to clear the form when the page is hit from that link you could add a param to the URL (e.g., clearForm=true) and use JavaScript to pick up the query string and clear the form when that parameter is present.
This is, of course, a purely client-side solution. For a server-side solution it would be helpful to know what framework you are using.

Categories