I'm using netbeans to create a web application that allows a cell phone user to scan a QR code and retrieve a journal's previous/next title information. The take home point is that there's no form that the user will input data into. The QR code will contain the search string at the end of the URL and I want the search to start on page load and output a page with the search results. For now (due to simplicity), my model is simply parsing an XML file of a small sample of MARC records. Oh, to top it off...I'm brand new to programming in java and using netbeans. I have no clue about what javabeans are, or any of the more advance techniques. So, with that background explanation here's my question.
I have created a java class (main method) that parses my xml and retrieves the results correctly. I have an index.jsp with my html in it. Within the index.jsp, I have no problem using get to get the title information from my URL. But I have no clue how I would get those parameters to a servlet that contains my java code. If I manage to get the search string to the servlet, then I have no idea how to send that data back to the index.jsp to display in the browser.
So far every example I've seen has assumed you're getting form data, but I need parameters found, processed and returned on page load...IOW, with no user input.
Thanks for any advice.
Ceci
Just put the necessary preprocessing code in doGet() method of the servlet:
String foo = request.getParameter("foo");
// ...
request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);
And call the URL of the servlet instead of the JSP one e.g. http://example.com/page?foo=bar instead of http://example.com/page.jsp?foo=bar in combination with a servlet mapping on an URL pattern of /page/*.
You can get the url parameter in servlet using request.getParameter("paramName");
and you can pass the attribute to page from servlet using forwarding request from servlet to jsp.
See Also
Servlet wiki page
Bear in mind that your JSP page will compile internally to a servlet. So you can retrieve the string and print it back in the same JSP. For instance assuming you get the string in a parameter called param you would have something like this in your JSP:
<%
String param = request.getParameter( "param" );
out.println( "String passed in was " + param );
%>
One last thing, you mentioned the main method -- that only gets executed for stand alone applications -- whereas you're talking web/JSP :O
Related
We have a form with 2 input fields.
These allow a user to type in destinations to plan a flight.
We want to use an external servlet that allows us to use autocompletion on the fields
(e.g. type "LO" and it gets the matches for that - London Heathrow would be at the top)
The servlet can be found at a URL like this: http://www.companyname.com/servlet/ac.json?n=12&q=LO
with "n" being the amount of results it should return, and "q" being the query.
How do I call that servlet from my HTML form, everytime the input field changes?
The servlet is on a different domain than my page, and I have absolutely no other access than calling it with the URL I posted.
The response from the servlet will always be a JSON string like this:
[{"type":"airport","city":"Cape Town","airport":"International","iata":"CPT","country":"South Africa","locationId":"airport_CPT"},
{"type":"city","city":"Chicago, IL","airport":"All Airports","iata":"CHI","country":"United States","locationId":"US_city_CHI"},
{"type":"airport","city":"Victoria","airport":"CA","iata":"YYJ","country":"Canada","locationId":"airport_YYJ"}]
How do I call that servlet from my HTML form, everytime the input field changes?
I strongly belive ,you need AJAX for this.If you use a normal servlet and use a form to submit each time the page will reload.
And coming to same servlet part,
You can specify the method names in AJAX request
You can try with JSP page in place of Servlet. You can call it very easily and it will complete your purpose also. Otherwise AJAX is best for this type of problem.
I have a JSP page to search customers. This page calls the controller, which execute a method to return a list of customers and after forward to the origin URL;
I used to forward : request.getRequestDispatcher(urlOrigin).forward(request, response);
(note 1: request.getHeader("Referer") was used to get complete origin URL )
(note 2: There a method to split the complete origin URL and get name page )
Since it, I have the following url in the browsear :
(http://domain/ProjetoT/mvc)
Its the url of my controller
If I search a customer again won't work, because the controller url will be recognized as origin url.
I Tried use : response.Sendredirect(urlOrigin);
But I lost my object and the list of customers didn't rendered.
Anyone can help me please?
Thanks!
Instead of intially accessing the JSP page directly in the browser, you could access it through the same controller used to process the search. To do that you would have to program your controller to detect if you are in initial display mode or if you are in "submit" mode. This is typically done by checking the presence of a parameter that is sent in the submit.
So in initial display mode, your controller would just forward to the JSP without any further processing, while in submit mode it would do what it is currently doing. This way you would be using the same URL for both the initial display and the submit and the problem you described should go away (that is, if I understand your question correctly).
What I want to do:
I have a form with a lot of fields(nick, email, name, surname, etc.) but the user has to fill Nick and Email first in order to be able to fill the other fields(this is because we want to check that the nick and mail aren't in use by another client before he can introduce the rest of his information(name, surname, etc.)).
So, the user introduces Nick and Email and then he must press a button named "Validate", if the values are available(successful validation) then the rest of the fields are enabled and the user can continue filling the form, otherwise the fields stay disabled and an error is showed to the user.
The form will be located in a JSP, it will be submitted to a Servlet, once in the servlet I must validate the information that is in the form(i have a .JAR file included in this servlet, the validation consists in calling a function from that library, the function returns a boolean) and then I must return back to the same JSP the boolean that will represent the result of the validate function.
Now in the JSP I must enable(or not, depending on the value of the boolean) the rest of the TextFields.
I'm not sure if this is right but i was trying to submit with the button and at the same time run a javascript(onclick) that will use this boolean value that the servlet sends back to the JSP after making the validation. The javascript consists on an IF sentence that evaluates the boolean and if it's true then it enables all the fields on the JSP.
Problems so far:
I was able to send the Nick and Email from the JSP to the Servlet and to make the validation of the values, now i have the boolean but i have no idea on how to send it from the Servlet to the same JSP and use it in the onclick event of the same button I used to submit the info. I don't even know if it's possible to do this...
I'd be grateful if someone could give me a hand with this, i'm newbie in Java programming so i would appreciate simple explanations if possible.
Also, if there is a better way of doing what i want please share it, and if there are any doubts ask and i will try to explain it better.
There is no need for JavaScript at all.
In your servlet you can store the validation result into the request context:
req.setAttribute('checkResult', checkResult);
where req is of type HttpServletRequest and checkResult is a Boolean.
Then you can forward to your JSP:
RequestDispatcher dispatcher = req.getRequestDispatcher("/your.jsp");
dispatcher.forward(req, resp);
In your JSP you can set your form elements as read only depending on the attribute checkResult which you have put into the request context:
<textarea name="text" cols="50" rows="10"
<%= request.getAttribute("checkResult") != null && request.getAttribute("checkResult") ? "" : "readonly" %>
>...</textarea>
So if the check is not valid then the <textarea> element will contain the readonly attribute. Otherwise readonly is not present.
As Roy mentioned AJAX is best suited for your problem. You can use DWR! , it makes normal java classes available as AJAX services, just call the method on them and get the result. So easy.
I think AJAX is more suitable for your application, which will not require to submit the whole form and you can send back the validation flag as plain responseText or well-formatted responseXML. Also you can use a lot of good javascript library such as jQuery that helps you send an AJAX request quickly and simply.
I want to pass parameters from one jsp to another.
I have tried using the post method, <jsp:forward/>, but it doesn't work.
I have created a <form> in html (parameters passed using POST), which is submitted to a servlet which processes the request and forwards it to another servlet that displays a page.
From this servlet i have created links to another jsp, passing through the parameters as GETs in the URL. However, I actually want to pass the parameters to another jsp using POST, and then pass it on to another jsp.
What solutions do you have or this problem?
Check out the Request Dispatcher. You need to forward the request to the landing JSP.
http://docs.oracle.com/javaee/6/api/javax/servlet/RequestDispatcher.html
Sounds like you are creating a multipage form that gathers information from the user across several distinct pages. In that case one option is to use hidden fields on a form to store the previous values. This of course means that as the pages progress the amount of data passing back and forth from client to server increases.
You may consider a server side approach by storing the interim values in a database for instance, then only passing a token back to the client. When the next JSP page is submitted, use the token to look up the values in the database.
JSP has built-in request object.when one jsp redirect to another jsp with some parameter, you can get parameter value using this request object.
<%
String param1 = request.getParameter("parameter_name");
%>
you can find example here -
http://www.roseindia.net/jsp/RequestObjectInJSP.shtml
Why don't you call a page on the click of a submit button by creating
an url in the below format (in javascript):- var
url="your_page_name.jsp?value1="+encodeURIComponent(document.getElementById("your_text_field_or_any_other_field_id"));
and then call the page by using your url
document.your_form_name.action=url;
document.your_form_name.submit();
and then use request.getParameter() method either in servlet
or in the jsp that u'v metioned in the url (servlet or the jsp u'll be
calling thorugh u'r jsp).
I need to call from one JSP to another, do some stuff over there..
I am inside the caller jsp, inside his handleReqeust(HttpServletReqeust request)
method I am trying to forward the request to another JSP, to call the other JSP file to his handleRequest(HttpServletReqeust request) off course with the request object
I tried it:
RequestDispatcher dispatcher = request.getRequestDispatcher("/theSecondJspFile.jsp");
if (dispatcher != null)
dispatcher.forward(request, response);
but to make it work I need for it the object response, but I don't have it,
I am sure I missed something basic, but what?
From my question you can see, I don't have solid backround in java, so please correct me, or refer me to good guide if you feel it necessary
Thanks.
-------------------Edit--------------------------------------
I don't need a redirect, I just want to call another JSP file to his handleRequest method
I think it relate to HTML
What I can make out of your question is that you need to redirect to a secondpage.html from firstpage.html with some data. You can use both GET or POST method to send the data.
For the GET method just redirect to secondpage,html?data=value. The data will be available in the HttpRequest parameter in the controller of the secondpage.html where it can be used as required.
For the POST method you would need to post the data (using a form on firstpage.html) to secondpage.html. In the controller of the secondpage.html the data should be available in a similar way as before.
To include another JSP in a jsp :
<jsp:include page="foo.jsp"/>
You can find some reference material here.
I can not be sure, but what I think you are trying to do is to include a jsp page on to your jsp page and use the the objects and other variables declared in the first jsp page in your second.
<%# include file="mypage.jsp" %> should help you do this.
If this is not what you are looking for, please make your question tad bit more clear. some code will help really.
JSPs are supposed to be used to present the final result. JSPs are not supposed to be part of business tasks leading to this result. There you use normal Java classes for, starting with a servlet class. Let the HTTP request (the one which you enter in browser address bar or specify in some HTML link or form) point to the URL of the servlet instead. This way you can write Java code in the servlet the usual way to invoke other Java classes/methods and finally forward the request to a certain JSP file based on the outcome of the result and then just let that JSP present the final result.
To start with servlets, I'd suggest to read our Servlets info page.
I just added empty Iframe, and set his URL when I needed to call him