I need a double value from my servlet to markup in my JSP. My doGet() is sending back formatted HTML tables with values from an ArrayList, so after I got that working I decided to tackle this part.
Servlet:
//Code getting the tables I need
//Send back the result, this all works good
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(returnAsHTML.toString());
What I added to try and get the double value
//Send back the result
double test = 20;
request.setAttribute("Test",test);
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(returnAsHTML.toString());
getServletContext().getRequestDispatcher("index.jsp").forward(request,response);
In JSP:
<!-- This variable is unresolved -->
<small>Test : ${Test}</small>
The forwarding seems to crash the whole party. I am new to JSP, I'm sure I am missing something small. I need to keep the response.getWriter() stuff there, it gets a lot of the information I need. Now I just don't know how to get my double values I need as well, because they will be displayed on a whole different part of the page.
You cannot write output to the servletOutputStream and redirect at the same time.
What do you expect from the browser: to display content or to navigate to another page? If the first, don't redirect. If the second, don't display HTML content.
Try using httpsession. Encapsulate whatever object you have using session.setAttribiute().
Then forward it using the sendRedirect() method.
Might be of this kind
HttpSession session=request.getSession();
double test = 20;
session.setAttribute("Test",test);
response.sendRedirect("Index.jsp");
On the JSP page
String s=(String)session.getAttribite("Test");
double d=Double.parseDouble(s);
Related
So, I have this Servlet that should send a parameter to a .jsp file:
request.setAttribute("parameter1", new BigDecimal(50));
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB/pages/page1.jsp");
requestDispatcher.forward(request, response);
However, when I try to alert this parameter with some javascript code inside the jsp, I get an empty message. Also, if I print the attribute to the console with Java using JSP Expression, I'm getting null.. So, I think I'm not correctly sending the parameter in this servlet, can someone help me here?
There are two ways you can accomplish this.
Using a JSP expression you would use <%= %> as (notice no ; at the end)
data : [<%= parameter1 %>, Y, Z]
The second and preferred way is to use JSP EL syntax and reference the request attribute directly using ${ } as
data : [${parameter1}, Y, Z]
The first option requires you to pull the attribute out of its scope first. The second doesn't.
BigDecimal parameter1 = (BigDecimal) request.getAttribute("parameter1");
SOLUTION:
I can't really see the difference, but just changing from:
request.setAttribute("parameter1", new BigDecimal(50));
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB/pages/page1.jsp");
requestDispatcher.forward(request, response);
to:
request.setAttribute("parameter1", new BigDecimal(50));
request.getRequestDispatcher("/WEB/pages/page1.jsp").forward(request, response);
allowed the alert(${parameter1}); to display the actual value that I passed, however, I still got null when reading the parameter with java. Also, some javascript code I had to display a chart stopped working.
So, I've tried to pass the parameters by session:
request.getSession().setAttribute("parameter1", new BigDecimal(50));
request.sendRedirect("/WEB/pages/page1.jsp");
This worked fine, now I can get my parameters both with javascript and java in the .jsp file.
It seems that redirecting to a new request using sendRedirect doesn't crash the javascript code in the .jsp file, however, I don't really know why this approach is working face the others, so I apologize for this poor answer, I've never really worked much with servlets and Jsp's but I really appreciate a more conclusive answer than mine, so I get to learn more too.
I am setting a session variable in doGet() like
HttpSession session = request.getSession(true);
session.setAttribute("MySessionVariable", "String goes here");
I am trying to display that in my JSP page upon ready like
<%= session.getAttribute("MySessionVariable")%>
and I am making a call to the servlet on ready like
<Script>
$(document).ready(function() {
$.ajax({
type : "GET",
url : "UserInfoDisplay"
});
});
</Script>
But what I see is that, the value is null for the first time and when I refresh again I get the value. That is the ajax call onReady and the page load happens parallely. How should I fix this? I want the data to appear on the first load itself.
Well, it's not exactly like that. Page load happens first, and when the page is loaded, it invokes the servlet. You have to take into account that <%= session.getAttribute("MySessionVariable")%> is executed server side, before the browser gets any HTML. Then, the browser gets the response and your script calls the server again and sets the property, but the page was already rendered. You have to do this in a different way. Can you explain what are you trying to achieve?
If you need it only for the first time, then why make a ajax call in the first place?
Ajax is meant for sending asynchronous requests. Correct me if I am wrong, the better approach would be to redirect a request to the servlet and get a response, isnt it?
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
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
1) In my servlet program, I have a statement which will be printed using the code as follows,
out.println("<b>This is servlet output</b>");
Instead of getting printed in bold, it just gets printed with the tag in the broswer itself.
How to rectify the same?
2) Also, in the servlet page after the submission of a jsp form, I want to add the below HTML tag inside the java code of the servlet program.
Go to JSP form
How to achieve the same? Please advise.
1) The browser is interpreting your output like text, try adding
response.setContentType("text/html");
This line tells the browser that you're sending HTML and that it should be interpreted that way.
2) The same as bold text
out.println("Go to JSP form");
On a related note, I'd suggest that none of your Servlet class directly write HTML content to the response page. Servlet are made to handle forms, and aren't easy to use when it comes to write HTML responses.
Once thing you can try is to write the response in a JSP page, then forwarding the request to the JSP so it can handle user output.
Here is a sample:
1) servet_output.jsp
<b>My bold test</b>
Go to JSP form
2) Your servlet redirects to the JSP page:
request.getRequestDispatcher("servlet_output.jsp").forward(request, response);
This way, your servlet handles the request, and the JSP takes care of writing the response to the browser.
Don't use servlets for html.jsp is the right place to use that.
just use
request.getRequestDispatcher("name.jsp").forward(request, response);
and write html code there.