I have 2 jsp pages, the first jsp will display images with link to the second jsp page.
<a href='/display.jsp?src=<c:out value="${photo.source}"/>'>
In the display servlet, i have the following coding...
String srcLink = (String) req.getParameter("src");
req.setAttribute("src", srcLink);
getServletConfig().getServletContext().getRequestDispatcher("/display.jsp").forward(req, resp);
}
Inside my second jsp (display.jsp), I have the following coding...
<img src="<%= request.getAttribute("src") %>" />
However, when I view in my browser, it will show as...
<img src="null" />
Is there any steps that I have done wrongly?
I guess,You hit with syntax error.
<a href='/display.jsp?src=${photo.source}'>
I have changed to the below and it is still not working...
<a href='/display.jsp?src=${photo.source}'>
Actually there is no error displayed on my first jsp page as I can see all the links being displayed correctly.
After much trying, I have removed the coding in my display servlet and changed the coding on my second jsp to...
<img src="<c:out value = "${param.src}" />" />
Now it is working fine. Thanks for the suggestion :)
You can try
<a href='/display.jsp?src=${photo.source} />
in your first jsp page.
No need to set the attribute as request.setAttribute(), as you are using the RequestDispatcher. It forwards the same request to other servlet/JSP. You just can use request.getParameter
use
request.getParameter("src")
instead
request.getAttribute(...)
so the code in display servlet, would look like:
getServletConfig().getServletContext().getRequestDispatcher("/display.jsp").forward(req, resp);
and inside display.jsp,
<img src="<%= request.getParameter("src") %>" />
refer: http://www.jguru.com/faq/view.jsp?EID=206736
Related
As we know every jsp program there is a servlet behind the jsp page. I have used a jsp page to make a form (its a very small form), and in the same jsp i used scriptlet tags and made a way to get the inserted form data, and display it using out.print(). but the problem is it when i run it, the form is displayed., but when i submit is, it doesn't recognize the servlet page (error coming as "The requested resource is not available"). i will put the code below., please help me friends to solve this problem. thank you.
i did this in netbeans.
jsp page name is- "hello.jsp"
the servlet page name behind the jsp page is: "hello_jsp.java".
<html>
<head><title>IF...ELSE Example</title></head>
<body>
<form action="hello_jsp" method="post">
<input type="text" name="y"/>
<input type="submit" value="submit"/>
<%
if(request.getParameter("y")!=null) {
String s = request.getParameter("y");
if(s.equals("hello")){
out.print("welcome"+s);
}else{
out.print("not welcome");
}}
%>
</form>
</body>
</html>
My guess is that you need to change
<form action="hello_jsp" method="post">
to
<form action="hello.jsp" method="post">
<!-- ^---- change is here -->
The externally-accesible resource is the jsp, not the servlet. (By default, I'm sure some config-fu could change that.)
Or, of course, if the page is supposed to submit to itself, don't include action at all. The default is to submit to the current page.
<form method="post">
I have a jsp page where am displaying image (which is generated in servlet and passed with name in jsp) and accepting text. Now i would like to pass that image tag value and textarea value to another jsp page. I am able to get textarea value but how to retrieve image to servlet.
JSP code:
<form id="form1" action="CallServlet">
<img src="PieChartDemo" width="600" height="400" border="0" name="chartsample"/><br>
<textarea rows="4" cols="150" id=t1 name=t1></textarea>
<input type="Submit" value="Send"/>
</form>
Servlet Code:
PrintWriter out=response.getWriter();
PieChartDemo pcd=new PieChartDemo();
String textvalues=request.getParameter("t1");
System.out.println("Text values : "+textvalues);
out.println(textvalues);
Now i would like to display or store it into a variable that image. How to go about? Suggest.
Thanks in Advance.
you can not upload files using getParameter method.
for that u will have to use apache file upload.
Just go through the following link it will definatley solve your problem.
servlets-file-uploading
(Oups.. Sorry for my english :) )
In my web application, Struts2 is used as the main Servlet dispatcher and filter. But For some reasons, i have a custom filter and a custom servlet used for a specific url "/book".
But I have some commons jsp... i had some issues when the custom Servlet should display my request attributes in the JSP because of the struts tags (implemented before). So i changed these tag by the jstl taglibs and it works now.
But... In one JSP, the main (lol)... I have a search form.. This JSP is included in several JSPs and could be called by Struts and the custom Servlet..
With only Struts the tag was "< s:form>.." and when the form was submitted, all sended values was kept in the input... But now, because of the custom Servlet i use a simple html form which is calling the struts action "search.do".
As source code is below:
<form method="post" action="<c:out value="${contextPath}"/>/search.do" name="search" id="search">
<input type="text" id="search_searchWord" value="" maxlength="200" size="100" name="searchWord">
<div align="right">
<input type="submit" value="Ok" name="searchButton" id="search">
</div>
<select id="search_searchCrit" name="searchCrit">
<option value="0">Crit1</option>
<option value="1">Crit2</option>
<option value="2">Crit3</option>
</select>
</form>
My problem is the search word and the selected option are refreshed after the submit. I need to keep them !
Is there a way to use the struts taglibs with a Standard Servlet ?
Or Do you have another solution to keep the submitted information ?
Thanks all !
take each field value from the input field and write js function to fill each field in jsp source code of your page.
function selectedValue(){
var value =<%=request.getParameter("searchCrit")%>;
if(value !=null)
{
document.getElementById('search_searchCrit').innerHTML=value;
}
}
I found a solution with the help of #java_seeker.
In my Struts action, i got the request through this way :
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("searchWord", this.getSearchWord());
There is two different way to do this, see: http://www.mkyong.com/struts2/how-to-get-the-httpservletrequest-in-struts-2/
The attribute is setted in each method (in the action) that could refresh the page.
Then, i just recovered and set the attribute from the request as a variable with a jstl tag and display it as the value of my html input:
<c:set var="searchWord" value='<%=request.getAttribute("searchWord") %>' />
<input type="text" id="search_searchWor" value='<c:out value="${searchWord}" />' name="searchWord">
For the , i just used an <c:choose><c:when test=""></c:when><c:otherwise><c:otherwise></c:choose> to set the selected choice.
Now all value are always displayed. Maybe it's not the very good way to display share the same JSP between a standard servlet and a Struts action, but it works. I'm open to try a better solution if you have one! Thanks all!
I'm trying to access form data that is filled out inside a jsp:included page from outside the page. I am using jquery and am open to using ajax. My code looks a little like this:
<form name=form1>
<jsp:include page="someFormData.jsp" />
//Other inputs out here in <% include %> files
<input type=button value=Submit onClick="return commonSubmit()"
</form>
I need to use the jsp:include style include because having everything on one page using
<%include...%> was causing an exception due to my jsp being too large. Alls I need to do is be able to submit the form and include the data inside "someFormData.jsp"
In my jsp page, there is link as follows.
<s:url var="editReqDetails" action="editReqDetails">
<s:param name="siteID" value="siteId"/>
</s:url>
when I click on that link, browser URL is
http://localhost:7101/legal/editReqDetails?siteID=99
like above.(The parameter shows in the URL.)
I want to know how to hide above highlighted part(the parameter) from the url.
If you can use javascript you could do this
<s:a href="#" onclick="window.location.href='%{editReqDetails}'">Edit Details</s:a>
This way you "hide" the url from the user. Though I'm not sure what the big problem is. If the user is malicious he can easily look in the source and get the values.
No, you can't use this. You pass parameter with http GET method that is default used in s:url tag and you want to get http POST method behavior. See the usage of struts url and choose one http GET or POST method.
You can do this:
<form id="edit-form" action="editReqDetails" method="POST">
<input type="hidden" name="siteID" value="siteId" />
</form>
Then:
<script type="text/javascript">
$(document).ready(function() {
$("#your-link").click(function(e) {
$("#edit-form").submit();
});
});
</script>