accessing variables from another form using javascript - java

in my current webpage (a JSP file which uses a form) i am interested in accessing variables that is autocalculated in the current webpage using a java class, from another form (also another JSP file) using javascript and DOM methods...is this possible?
Form A contains a link to Form B and i would like to put a javascript method in Form 2 to get variables/information in Form A.
EDIT 1: just updated...i'm trying at the moment to make context path URLs to each different web page and save them as strings in a database. And in the Form 2 webpage, i will retrieve the string URL context path of Form 1 from the database and then somehow try to access that...still figuring that part out..
EDIT 2: this is not involving Parent-Child window, but a relationship between 2 webpages that uses a FORM element

Related

how can i retrieve the data from a jsp form to the java controller class(the name of input fields are retrieved from the database table

How can I retrieve the data from a Jsp form to the java controller class(the name of input fields are retrieved from the database table.
I tried to get that using the request.getParameter() method but I cant specify the a particular name of the field in this method as I retrieve the data from the database table in the jsp file
This is the image of the Jsp file from which I try to get the data into the controller class.image
If you don't know the names of the parameters, you can discover them by calling request.getParameterNames(), then iterate over each of them to find their values.
Step 1: Try to create a hidden value that will stash the values of the input elements came from the database.
Step 2: This hidden value will be populated by javascript / jquery on form submit.
Step 3 Java controller will interact with the hidden field not on the actual elements using request.getParameter("hiddenFieldName");

Navigate results in content space

I hope you get my problem.
out.println("<li class='has-sub'><a href='#'>" + k1.getKName() + "</a>\n");
I have a JSP and inside this java code. The result is a navigation on the left side with several categories and subcategories. So this is one category element. As you can see, I didn't put anything in the href. What I want to do is, that when I click on this category, I will get the articles of this category in the content space on the right side.
So, what do I have to do with servlets or JSPs in order to give a result to the content space. I can't just call a servlet there of course, because that means that I get the result of the servlet inside the href obviously.
I am sorry if this is a silly question, but I really don't know how to solve this :(
Further to previous comments you do not need web services. You can do this using ajax and a normal Servlet. You might want to look at using JQuery to help with the Ajax part. Here's some JQuery documentation around the load() function which will:
Load data from the server and place the returned HTML into the matched
element.
https://api.jquery.com/load/
Your link will look something like (if k1 is a bean in some scope then you can use EL rather than scriptlets):
<a href='javascript:loadData(${k1.id});'>${k1.name}</a>
Your Javascript will look something like:
function loadData(id){
var url = "/pathToMyServlet?id=" + id;
$( "#result" ).load( url );
}
which will call your Servlet and insert the HTML returned to an element on your page with the ID 'result'.
Your Servlet then needs to generate the data and forward to a simple JSP which returns the results (and only the results) i.e. it does not need to be a fully formed HTML page but should only contain the table of results or whatever.
And stop using scriptlets:
How to avoid Java code in JSP files?

Calling java search function in JSP

I have a database setup with hibernate, and I want to call a search on the table in my JSP page. How to I call my java function with the jsp page and store the results, then print them on the jsp page?
Search function (Search by subject)
public Iterable<Layouts> getSelectedLayouts(String subjectName){}
I have the class containing this method in my jsp page as a spring bean
custom:useSpringBean var="layoutManager" bean="LayoutManager"
where LayoutManager is the class
I don't know how to call the method in the jsp page and output the results
I'm using a spring/hibernate to connect to the database, and running my jsp on a local tomcat server
Since it's not clear from your question as to what framework you're using. I will just give you a very general and simple answer but you can use the same idea in any framework you use.
You probably have a button in your jsp page which when clicked will invoke a servlet. Inside the servlet, you will have access to the fields that were submitted via the jsp page. Use the fields to create a search object(DAO) and query the database with it.
When you get results from the database, save it in session scope. So now in your jsp page, you can access the object and display it. You can use jstl to access the fields of the object.

small program on struts2 and jsp

i have done some learning on struts based on one project that i got.Now i have to build 2 to 3 struts jsp pages.
I have following scenario..
<action name="BackAction" class="ClassnamePath">
<result name="user_validated" type="redirectAction">
<param name="actionName">welcome</param>
</result>
<result name="user_profile_found_in_database">/resources/userprofile.jsp</result>
what does param will tell..what is the significance of param(i do not know about this)
my scenario is like this In the class(ClassnamePath) i have done one java program which bring the data from the database and put the values in the userprofile.java(for example the userprofile has variable members like name,email,phone,pin)
that values are come from database and stored in the object of the class userprofile.
i have a task that whenever the result "user_profile_found_in_database" has been done those values should be presented in the jsp as result of the "user_profile_found_in_database"
Is the result "user_profile_found_in_database" that i mentioned in the action tag correct?
the jsp page should be having userprofile with labels as the fields of the userprofile and values should be in the text boxes.
i do not know anything about jsp pages...even web programming..but i am learning on my own..(i am having one doubt how jsp pages are different from struts jsp pages)
with the above tasks i can learn lot in struts and jsp..
Please give some knowledge on this to build further.
JSP pages are not way different than simple JSP pages and in short Struts2 will provides a set of tags which will help we as an end developer to build application fast as these tags provides a easy to access functionality about various features S2 providing, few of them are
Accessing Value Stack using OGNL
Data conversion from server to client and other way around
Features to access other things like request/response/session in most easy and flexible way.
In end when you browser will render the jsp page it will be simple HTML and S2 tags one using in there application will be converted to the HTML as browser will understand HTML.
regarding second part accessing user profile in your jsp do the following gin your Action class
Create an instance of UserProfile in you action class
Create getter and setter for the UserProfile instance
Fill the value in the user-profile (you will fetch that from your DB call)
when your action will send back the response, S2 will place the user-profile instance on top of Value stack and we can access its properties using S2 tags like
<s:textfield name="user_name" value="%{name}"/>
<s:textfield name="user_age" value="%{age}"/>
here name and age are the properties of your user-profile.param in your redirectAction configuration is being used to provide parameters to the result, for more information about what parameters do please read official document.
redirect-action-result
.
Ya i got your question like this,
what is the exact meaning of param tag?
param tag means:
Struts 2 “param” tag is used to parametrize other tags. However, when you declared the “param” tag, the “parameter value” can be define in two ways :
“value” attribute
Text between the start and end of “param” tag.
For Example:
<param name="fruit">Banana</param>
<param name="fruit" value="Banana"/>
In Your example the 1st case i think
Here see this web site it is present complete structure...
http://www.mkyong.com/struts2/struts-2-param-tag-example/

hi i have a field called DECISION in my child jsp how can i get that value to parent jsp

hi i have 3 jsp pages 1.parent 2. child 3.grandChild
how can i access a radio button in grandChild jsp in Parent Jsp
can i access like this
parent.parent.document.forms['AccountClosureForm']['DECISION'][0]==true
I assume you either used page include directive or jsp include directive. I don't see any parent child relationship here. You would be able to share the whatever you have inside request or session or application scope. And I suppose you would have been using request, most probably.
From your code snippet, it looks you want to use it in JavaScript. Isn't it? If thats the case you can simply do something like document.forms["whatever"].... because its gonna be a single document on the client, no matter if you used a number of includes.
you cannot
JSP is run server side, you dont have a Javascript document on your server
If you want to have jsp 'talk' to each other you can use session and request objects
But once all 3 jsp are send to the browser and rendered as HTMl you can use any JavaScript you see fit to achieve what you want

Categories

Resources