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");
Related
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?
I have to display the action message as html format.but the action message takes the html code as string.how to print that html in spring mvc.
Below is my code:
#RequestMapping("")
public ModelAndView openIndex(HttpSession session){
String msg="<html><span style="color:red">Invalid User</span></html>";
return new ModelAndView("index","msg",msg);
}
when i print the msg using jstl it displays like below
<html><span style="color:red">Invalid User</span></html>
But it should be print like below
Invalid User
How to achieve the above result?
Any help will be greatly appreciated!!!
You would be using tag in the jsp.Set the escapeXml attribute of to false.The escapeXml attribute determines whether characters <,>,&,'," in the resulting string should be converted to their corresponding character entity codes. Default value is true.
<c:out value="${msg}" escapeXml="false"></c:out>
Or directly use:
${msg}
on your jsp.
You can create one div on JSP page with.
Then set div background color as red
String msg="Invalid User";
return new ModelAndView("index","msg",msg);
On JSP page, you can simply get the value using ${msg}
OR
1.
String msg="Invalid User";
return new ModelAndView("index","msg",msg);
use jquery and in document.ready() function you can set msg as a div html
Something like $('#divID).html(${msg});
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.
I need to send a particular parameters to Servlet from JSP page. E.g: if I click on the Facebook icon on the web page, then I should send "facebook" as parameter to my Servlet and my Servlet will respond according to the parameter received from JSP file or HTML file.
This is a very open ended question, but the easiest way is to specify parameters in a query string.
If you have the following servlet:
/mysite/messageServlet
Then you can send it parameters using the query string like so:
/mysite/messageServlet?param1=value1¶m2=value2
Within the servlet, you could check your request for parameters using getParameter(name) if you know the name(s), or getParameterNames(). It's a little more involved, specifically with consideration to URL Encoding and statically placing these links, but this will get you started.
String message = request.getParameter("message");
if ("facebook".equals(message))
{
// do something
}
Storing links with multiple parameters in the querystring requires you to encode the URL for HTML because "&" is a reserved HTML entity.
Send Messages
Note that the & is &.
Just wrap the icon in a link with a query string like so
<img src="facebook.png" />
In the doGet() method of the servlet just get and handle it as follows
String name = request.getParameter("name");
if ("facebook".equals(name)) {
// Do your specific thing here.
}
See also:
Servlets info page
One way is to have hidden form variables in your jsp page that get populated on click.
<form action="post" ....>
<input type="hidden" id="hiddenVar" value="">
<a hfref="#" onclick="doSomething();">Facebook</a>
</form>
<script>
function doSomething() {
var hiddenVar= document.getElementById('hiddenVar');
hiddenVar.value = "facebook";
form.submit();
}
</script>
This gives you flexibility to control what gets passed to your servlet dynamically without having to embed urls in your href
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