Jump to content
SubSpace Forum Network

Recommended Posts

Posted

Ok so i got a question i need answered...

 

Im working on a .jsp file that is being served over apache tomcat webserver. I am trying to incorporate an additional functionality into from a different web interface. I pulled the Java classes i needed from this other place, put them into the directory im working on, and incorporated the java as a scriptlet <% and %> tags into the jsp file.

 

I need that portion of java to run upon a certain button being clicked on the web page, however, heres the problem i realized: Java scriptlets in a .jsp get compiled as soon as the page is served, run, and then are done. Meaning even though this scriptlet is within the function that is called by the buttonclick, it only runs when you first reach the webpage, and never after when you click the button.

 

Anyone knowledgeable about this and can propose a solution? Can I put the java scriptlet in an external file or script or something and then call upon that within the buttonclick function?

Posted
All I can say is you're correct, JSPs are translated into .java files, then compiled to *.class files. I couldn't follow the rest of the detail based on your comments, I'd think typically any dynamic changes would occur through javascript, or a POST/GET call to another action java class. Maybe someone else here can help, my j2ee knowledge has been lost over the last couple of years though I've tried getting back into it lately.
Posted

Ok i decided upon a different solution, but now have a new question/problem... so see if you can figure the following out:

 

One .jsp page will have a button, that upon clicking, redirects the user to a second page, which is identical to the first, with the addition of a java scriptlet (<% and %> tags) thats immediately runs when the page is served, performing the operations i needed. However, there are several text fields and such that i need to maintain their content when I forward to the second page. How can i transfer data when i use "java script:window.location='newurl'" ???

Posted

Here are 2 ideas:

 

  • Have your script change the action attribute of your form, submit the form. Something like:
    function bla() {
     var form = document.getElementById('myform');
     form.setAttribute('action', 'page2.jsp');
     form.submit()
    }


  • When the button is clicked perform a XMLHTTPRequest using javascript(ecmascript) to your second page, this way you never leave the page you are on

Posted

Iirc,

 

you need to put the input button in a post action form and pass a hidden parameter that you can retrieve in the doPost() method in the backing HttpServlet.

 

 

With jsf it's more like ASP, where the button name is used as a method like button1_click(..), but jsp is more primitive and everything will have to go through doGet or doPost with the exception of property binding with el language like ${firstname}

 

 

I've attached the jsp part of my course i have to learn for the exams in a few weeks, maybe it'll be helpful

Posted
Can you explain what that is doing in a little more detail jowie? I'm trying to do something this is above my head but i need to try, so im picking it up as i go

 

You probably know all this but javascript/ecmascript/jscript has nothing to do with java. The name javascript was nothing more then a marketing deal between Netscape and Sun.

With your Java code you are dynamically generating the HTML code. This is then parsed by the browser, after that your javascript code which is contained in your HTML code is executed by the browser.

You can only execute java code on your server when the browser makes a request to a page.

 

 

I am assuming you are talking about my first solution.

 

Somewhere in your HTML code you probably have something that looks a lot like the following:

<form action="page1.jsp" id="myform">
<p>
	First textbox: <input type="text" name="a" id="textboxA"/>
	Second textbox: <input type="text" name="b" id="textboxB"/>
	<input type="submit" value="Submit!"/>
</p>
</form>

 

You then have your second button somewhere

 

<input type="button" value="Second button" onclick="bla()"/>

 

And the following javascript code between your

tags

 

<script type="text/javascript">
function bla() {
var form = document.getElementById('myform');
form.setAttribute('action', 'page2.jsp');
form.submit()
}
</script>

 

 

 

 

 

 

 

Alternatively, you could get rid of the second page and add a hidden form field and only execute your code if that form field has a specific value:

<form ...>
<input type="hidden" name="secondbuttonused" value="0" id="secondbuttonused"/>
...
</form>

and the following javascript code:

<script type="text/javascript">
function bla() {
var form = document.getElementById('myform');
var secondbuttonused = document.getElementById('secondbuttonused');
secondbuttonused.setAttribute('value', '1');
form.submit()
}
</script>

You then only execute your code if the POST / GET variable "secondbuttonused" has a value of "1"

 

 

 

 

 

 

 

If you do not have a form you have to read out all the value's using javascript and append it as GET variables to the url of page you are about to go to.

Something along the lines of this:

<script type="text/javascript">
function bla() {
var textboxA = document.getElementById('textboxA');
var textboxB = document.getElementById('textboxB');
window.location = "http://www.bla.bla/page2.jsp?a="+escape(textboxA.value)+"&b="+escape(textboxB.value);
}
</script>

Posted

Ok i figured I'd post a full explanation of the situation...

 

It is a form that will submit some stuff to a database. The stuff can consist of several text fields (title, description, contributor, etc) and a multimedia image or video or file. At one point in the form, the user has an option of having a certain operation performed (which is what i need to implement). This operation relies on a few java classes that I have. I believe the only way to do this operation is thru a scriptlet since it calls on several java classes, does some string manipulation, etc. However, this scriptlet needs to only run when and if the user clicks a certain button showing that they want to perform this operation.

 

So i figured have a second form page that is the same as the first one, and one the button is clicked, forward to it, and the SECOND page will have the java scriptlet in it, which runs immediately (as scriptlets have to).

 

So my problem was maintaining all the text fields and such from the first to second page. However, i now have another problem. I need to use the final string i create in the scriptlet outside of the scriptlet. To do this I understand i can use pageContext.setAttribute() which default puts the attribute as pagescope. Then outside the sciptlet call the getAttribute() to retrieve it. For some reason this is giving me null though.

 

In addition, i realized if a user refreshes when on the second page (which is identical to the first page but has the scriplet to run the operation i want at the begining), it re-performs the java scriptlet :/

 

How should i approach this?

Posted

I've written a small example in netbeans with tomcat 6.0.

 

The page flow is as follows:

 

index.jsp --post--> BackingBean (do stuff in database etc) ---redirect and forward request params---> result.jsp

 

I used EL language in result.jsp (the ${param.FirstName}) as a shorter notation than <% request.getParameter(...) %>

 

 

Hope this helps ^^

ExampleJSP.rar

Posted

Well im down to two specific problems now, so I'll ask one for now

 

I do a session.setAttribute("result",results) within scriptlet tags because i need to access results outside of the scriptlet. Outside of the scriptlet i tried the following things:

 

var word = <% session.getAttribute("result"); %>

var word = '<% session.getAttribute("result"); %>

var word = ;StringFormat.toHTMLString((String) <% session.getAttribute("Result"); %> )';

 

the first doesnt run, the second returns null/undefined, and the third returns an empty string. I CAN however do

<% JOptionPane.showMessageDialog(null,session.getAttribute("result"); %> and see the content I want perfectly.

 

How do I correctly store the Java string associated with result into a HTML var?

Posted

the <% %> tags indicate that the block will be replaced with whatever that is outputted

 

meaning

 

var word = "<% session.getAttribute("result"); %>"; should do the trick

 

i think it's also possible to use

 

var word = "${session["Result"]}";

 

I don't know the exact EL language, but netbeans has intellisense with it

 

 

Perhaps verify it with alert("<%...%>") to see that it's not another problem

Posted

var uses single quotes not double so its

var word = '<% session.getAttribute("result"); %>'

 

I looked into the EL way and that would be var word = '${result}' I believe

 

Neither of these works :/ and i deem that by doing alert and them giving me blank strings or undefined.

However I have not bothered to do alert(<% %>) I have only stored the thing in a var and then alerted the var. I will try that today.

Posted

try ${sessionScope.result}

 

 

Ah crap, i just noticed

 

var word = '<% = session.getAttribute("result"); %>'

 

don't forget the = sign before the expression because you want it to out.write()

Guest
This topic is now closed to further replies.
×
×
  • Create New...