HTML Elements to multiple servlets? - java

I have 2 JSP's pages as well as 2 servlets.
I want to get what ever the user enter in the username text box and store it in 2 scriptlets and then show it on the 2nd JSP page.
Example:
JSP Page 1
<input type='text' name=user>
Servlet 1
String userName = request.getParameter("user");
Up to this point im good I understand.
But what if I want to carry over that same value to in this case a 2nd servlet and then from that servlet display the same value to another jsp page?
I tried setting and getting the attribute like this,
request.setAttribute("userName", userName);
RequestDispatcher dispatch = request.getRequestDispatcher("SecondServlet");
dispatch.forward(request, response);
And then casting to string in the 2nd servlet how ever when I place that value in a scriptlet tag it does not display anything on the jsp page?

Related

Query String not retrieving values in servlet page

I am sending two values by using Query string using both jsp and servlet pages.
I need to taking only one value from query string in jsp page working fine.
At the same time in servlet page when i click button i need to get the value
from query string but here i am not getting the value from query string.
which value is i need to took in jsp that value only it visible remaining value
is appear in servlet page.
how can i retrieving query string values in my servlet
In jsp i am using query string at the text box value code:
<input type="text" id="txtBatchName" name="txtBatchName" value="<%=request.getParameter("BatchId").replace("id=","")%>">
Servlet page:
if(request.getParameter("btnUpdate")!= null){
String Batch2=request.getQueryString();
String Id1=request.getParameter("Id");
}
Try with name attribute.(Id is specific to Java Script and it wont be transmitted to server side)
String Id1=request.getParameter("txtBatchName");

Perform an action on JSP if redirected from a servlet

I want to perform an action on a JSP if redirected from a specific servlet only else do nothing.Is it possible?
In my JSP there are different errors defined. This JSP calls a servlet (with contentType as application/pdf) which opens in a new tab and searches for a PDF for 25 seconds and then if PDF is not found redirects to same JSP which shows the error message "File not found". I want to show the error if called from servlet only else do nothing.
JSP Code:
<%}else if(hPP!=null && hPP.get("errorcode")!=null && hPP.get("errorcode").toString().equalsIgnoreCase("Issue")){%>
<c:if test="${cameFromServlet}">
<div class="SplInputField">
<label class="FontBlod">Download fail</label>
</div>
</c:if>
servlet code
if (content == null) {
request.setAttribute("cameFromServlet", true);
String redirectJspUrl = request.getParameter("homeRedirect");
String strReceiptPage =
redirectJspUrl.substring(0, redirectJspUrl.lastIndexOf("/")) +
"/GetQReceiptPage";
response.sendRedirect(strReceiptPage);
}
Add an attribute to the request in the servlet like this
httpservletRequest.setAttribute("cameFromServlet", true)
then in your JSP check for it
<c:if test="${cameFromServlet}">
DO STUFF HERE
</c:if>
EDIT:
What you have done in your edit will not work, since you are doing a redirect. Which means the browser is sent a 302 response to tell it to issue another request against the new url. Do you have a specific requirement to change the url for the user? If so you will need to add the cameFromServlet attribute to the session instead - like this:
req.getSession().setAttribute("cameFromServlet", true);
Bare in mind though, that cameFromServlet attribute will remain on the session until you unset it so if there was another time that jsp page is shown you will run into problems unless you do something to unset it - either by introducing another servlet in the middle and moving it from the session to the request - thus simulating Springs flash map behaviour or unset it in the JSP after you have used it - like this:
<c:remove var="cameFromServlet" scope="session" />
If you do not need the URL to change for the user, you can change your servlet code to make use of a request dispatcher (what I thought you were doing)
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/yourjsp.jsp");
requestDispatcher.forward(req, resp);

return from servlet to jsp to specific id in index page

I am working on java servlet project
I have index.jsp page that take input from user and send it withe post method to readXml servlet page that process the input then return the result to index.jsp
I wont to return to specific id in index.jsp
I used this code for foreword response to index.jsp
request.setAttribute("message", span );
request.setAttribute("Sent", input );
request.getRequestDispatcher("/indexjsp.jsp#color").forward(request, response);
request.getRequestDispatcher("/indexjsp.jsp#color").forward(request, response);
when I use this code I get resource error
and when I remove #color it will return the response to index.jsp but not with id I wont.
are there any way to return to specific id from servlet?
DO it something like this
instead of using #color try ?id=color.
Populate this parameter in the hidden input tag on 'index.jsp'. Now you have your tag id on client side. you can do what you want using javascript after onload event triggers.

Display HTML page after servlet is finished

I have a Servlet which accept username and password and authenticates it.
After it is done, I want to display an HTML page.
It is a big HTML page so I read that using writer.println() was not a good idea.
How do I display the new page?
I tried using sendRedirect but that, for some reason, displayed the same page (login page) again.
Please help.
Create a JSP page contains your HTML, then use requestDispatcher to forward the request.
for Example:
if you created a JSP page named MyJSP, then in login Servlet use:
request.getRequestDispatcher("MyJSP.jsp").forward(request, response);
you can use this lines of code
create an html whatever the name you like i had created LoggedIn.html and
use this code in your servlet
ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/LoggedIn.html");
rd.forward(request, response);

show data from servlet in jsp but without override the current value

I need to display the data from a servlet in a jsp page.
At the moment I have this:
SERVLET:
Object data = text;
request.setAttribute("data", data);
request.getRequestDispatcher("index.jsp").forward(request, response);
JSP:
<p>result: ${data}</p>
In the JSP there is a simple text box and a send button.
At the moment when I push the send button the response is being overwritten ervery time.
How can I do it that after every search I see the result in a new line?
I want also see the previous searches...
Thanks a lot!
You've got a couple of options:
Send over the current value to the server, do the search and append the new result there, and send back the whole string to the JSP in the Request to display it all again. You'll have to wrap the value in an <input> tag, possibly <input type="hidden"> if you still want to show data in a <p> and not as an input field.
JSP:
<input type="hidden" name="oldData" value="${data}"/>
<p>result: ${data}</p>
Servlet:
Object newData = text;
Object oldData = request.getParameter("oldData");
request.setAttribute("data", oldData + "<br/>" + newData);
request.getRequestDispatcher("index.jsp").forward(request, response);
Store all values of data in the session scope, and just append to it from your servlet. The JSP would have to output the value from the session scope instead of the request. In this example values are stored in a unique concatenated String. It woukld probably be nicer to store each value of data in a data structure, such as a List, and just iterate over it in the JSP so the separator remains in the view.
JSP:
<c:if test="${not empty sessionScope.data}">
<p>result: ${sessionScope.data}</p>
</c:if>
Servlet:
Object newData = text;
Object oldData = request.getSession().getAttribute("data");
request.getSession().setAttribute("data", oldData + "<br/>" + newData);
request.getRequestDispatcher("index.jsp").forward(request, response);
1 .Use an array instead of a simple object in servlet
Populate the array with the new values :Servlet
Traverse the array and display each item where ever you want to display as new lines
You are putting your data in RequestScope you have to add them into the SessionScope in order to see the previous results.
See this example: http://viralpatel.net/blogs/jsp-servlet-session-listener-tutorial-example-in-eclipse-tomcat/.
It is not very good practice to write java code in JSP, so you would want to move the logic into servlet. But this example describes the point you need.

Categories