jsp function call and xml - java

i need to know the way for call a function in jsp?
in my function, i wrote a code for get user input and write it in xml file... when i call it, there is a error... how could i achieve this.?
<form method = "post" action="Result.jsp" >
<input type="submit" name="submit" value="Submit" onclick="writeXml()"/>

Place your function (method) in a class that extends HttpServlet
map your servlet in web.xml
make action="/yourServletMapping"
process the submit in the doPost(..) method
But first, read some servlets tutorial.
Update: place the jena jars in WEB-INF/lib

You are trying to call a java method in HTML/JSP.
This cannot be done.
When you write a JSP, and "access" it in a browser, the server (like Tomcat) will "process" the JSP and pass the "output" to the browser. The browser sees only HTML/CSS/Javascript and no java code.
The onclick is called by the browser, so the java method cannot be called here.
You need to submit the form to servlets - something like "pass control to servlets" and from there you can call java methods..
Write a servlet. In the onclick event, submit the form. And follow Bozho's advice. (As he said, please read some tutorial on Servlets)
EDIT:
BTW, the exception you had mentioned is NOT because of this. There is something else wrong. And to find out what is wrong, we need more details from you. Apart from the JSP what else do you have? Read the complete exception stack trace. Does it mention any of your classes?

Related

Passing JSP data to Java class and vice versa on button click

I am fairly new in JSP and I am trying to figure out how to pass data entered in the form on a JSP page to the java class and send this data back to the JSP page on click.
My index.jsp looks a bit like this:
<%# page import="mypackage.*" %>
<% myClass c = new myClass();
c.setString("String"); %>
<p>This is a test: <%= c.getString(); %></p>
The above code will output "String". I can access my class with no problems if I set the value on page load. I tried using servlets after some research. I modified my form to add the servlet "testServlet" on form action:
<form method="POST" action="testServlet">
Then on the doPost() method in testServlet I added in this:
String myString = request.getParameter("myString"); //myString is also the name of my textbox
System.out.println("Entered string: " + myString);
However, I am clearly missing a crucial part of the flow of how this should work and I am most probably wrong with this one as well as all the form does after I press submit is redirect to testServlet so I get the error that the resource is not available since it isn't a JSP/html page.
So my questions are, how exactly can I pass data from JSP to java and vice versa? Also, is there a possible way to do this without servlets? And what are good tutorials/examples for studying JSP and its behavior such as passing data? Please help.
After some more research, I am now able to transfer data set on index to detail using jsp:useBean however, as I've read in all the forums I've visited, servlets should be used to handle this but this really confused me more as a beginner so I really want to figure out what I'm missing here.
I've made sure that the servlet is registered on web.xml and I didn't change anything in there.
Update: I've tried re-creating the project from scratch and somehow managed to make the servlet work. As it turns out, I was missing something on the doGet() method. But now my question stands, is there a way to process the form without using servlets or page import in the JSP file? I was shown a sample code that didn't use servlets or page imports. I did take notice at tag.
use the java beans with getters and setters for there class member variables.

call a java method when Click on a html button without using javascript

i work on JSP and i want to call a java method(Function) on Click on a html button without using<script></script>.how?
i try to write this code:
<button onclick="<%po.killThread();%>">
<font size="4">Kill</font>
</button>
but it doesn't work... so please help me.
thanks
You're misunderstanding how server-side programming works. When you load that page, the webserver will get to the line <button onclick="<%po.killThread();%>"> and will immediately parse and execute the JSP snippet, in your case po.killThread(), and replace everything between the <% and %> with the return value of that method, if any. And all these happens on server side, before client receives any thing. (Note that this will only happen if that page is not already been loaded and compiled into a Servlet by the server.)
Thus, the HTML that client receives, will be something like, <button onclick="some return value or nothing">, which means that nothing will happen when you press the button. If you want to execute further JSP commands on the button press you will need to make a new request to the server - for example, by redirecting the page.
This will call the function killThread when you open the website.
Try to redirect to another jsp which calls the function.
this will not run at all because after the jsp page is compiled it will return the po.killThread() value but will not call this method
You can see this by viewing the page source
JSP is a server-side technology. Did I say server-side?
In order to understand how JSP works and to clear any misconception, JavaRanch Journal (Vol. 4, No. 2): The Secret Life of JavaServer Pages is a very good read.
An excerpt from the same,
JSP is a templating technology best-suited to the delivery of dynamic text documents in a format that is white-space agnostic.
Template text within a JSP page (which is anything that is not a dynamic element), to include all white-space and line terminators, becomes part of the final document.
All dynamic elements in a JSP are interpreted on the server and once the document is sent to the client, no further dynamic interaction is possible (short of requesting the same or another document).
If you are using JSPs, then to perform some method calles, you will have to write a servlet and then call the method in doPost or doGet method of servlet.
On the other hand, if you want to make things simpler, use JSF framework which will help you achieve your objective as JSF supports event handling.

Communicating between JavaScript and Servlet

In my JSP page I have DIV.
<div id="100">
ALI
</div>
When I click on this DIV...
$("#100").click(function(){
});
...I need to send the value of the id 100, to a servlet, so that the servlet makes some database java codes, and returns for example, either 1 or 0. How can I do that? and is this the proper way?
Using Ajax, you should call your server using a URL similar to this:
http://localhot:8080/youAppContext/yourServer?id=100
Then, in the servler side, you should retrieve the value that will be in the request with the name "id"
There are out there many tool as jQuery that can help you to do the Ajax petition.
Edited
Well, here you can find a very simple Ajax example using jQuery. In the example, instead call a file (test1.txt) you should invoke a URL (as I described above). Of course, you will need to write some JS code to build your URL (where id be a variable). Once task is done in servlet side, you can return whatever, for example: "done" and display or not this information the HTML as it is done in the example.
Take a look to this Web, there are many links that can help you.
get the value using
var value = $("#100").html();
and pass it to servlet using AJAX

can ajax call other ajax page?

I have index page,which contain div section.i want to call different pages into this div section on the basis of her-link click,this is done. when calling pages that may also using ajax call,in this case ajax call not work for this page.please tell me it is possible to call ajax inside an ajax.if yes then please help me.
Update Edit:
I have created a gist where i have include all used pages,script https://gist.github.com/2786811.page service_status called the view.jsp using ajax call every 5 second,on hand view.jsp interact with database and prepare view.when we do ajax call from index.jsp, service_status called but it doesn't called view.jsp file.
Thanks
You can make ajax call on page load using $(document).ready(function () {...}); in target page you are calling.
If you are trying to dump a <script> tag with innerHTML, it won't work.
However, you could (as an example) return a JSON object with two properties:
{
html: "All your <b>HTML</b> here",
js: "alert('JavaScript to be run');"
}
Then, use the html property to put content on the page, and then eval the js property.
This isn't the best solution, since it uses the evil eval, but unless you want to rewrite your whole basic structure to use callbacks properly then it's probably your best bet.

sending html form data to java application

Is there any possible way to send the html form data to java application without using php and asp stuff?
I know we can do this using php and do it before but can i do it directly?
I had used php in my previous app in which user sends his data to php form that saves it into the data base but now i want to directly get the data from the html form.PLease any idea for that?
Maybe you could consider using Java Servlets and JSP for web-based data processing ?
use the html form action attribute to specify an endpoint that will hit a java servlet running inside of a servlet container.
To handle the request in your java class, implement the HttpServlet interface.
http://download.oracle.com/javaee/6/api/javax/servlet/http/HttpServlet.html
If you are posting from a form, them most likely you will want to implement doPost. Or you can implement service as a catch-all
Example:
<form action="/path/to/Servlet" method="post">
<input type="text" name="foo"/>
</form>
....
doPost(HttpServletRequest request, HttpServletResponse respnose) {
// set String foo to the form element named "foo"
String foo = request.getParameter("foo");
// now do whatever you need to w/ foo
}
Try Tomcat with Java Servlets. You need to:
write a class that extends HttpServlet
override the "doPost(HttpServletRequest, HttpServletResponse)" or "doGet(...)" methods
write a web.xml file mapping the web page URL to the servlet handling the request
compile and bundle everything together as required.
It'll take a little doing to get everything in the right place but it's not too hard. See the Tomcat documentation for further details. Good luck.
You can send the data by using jsp or by sending it in a link like www.google.com?q=usa
and parse it on other side

Categories

Resources