Image parameter in JSP - java

Hy!
My site calling:
http://localhost:8080/WebTest/index.jsp?image=1.JPG
Code:
<img src=<% request.getParameter("image");%> alt="1"/>
The problem is that no image is shown. The image is the right parth.
This is working:
<img src="1.JPG" width="3872" height="2592" alt="1"/>
Please help

The <% %> won't print anything. Use <%= %> to print something.
<img src=<%= request.getParameter("image") %> alt="1" />
It's safe to put quotes around the attribute value
<img src="<%= request.getParameter("image") %>" alt="1" />
But much better is to just use EL instead of the since a decade discouraged scriptlets:
<img src="${param.image}" alt="1" />

It's better to use
<img src="${param.image}" />

Related

Modify href between jsp

i need some help with these
I have header.jsp, login.jps and register.jsp.
I build my Body with Header.jsp and another JPS.
I want to change my 'Help' link of Header.jsp depending on the Jsp i build. How i can do that?
I looked for javascript solution but is a 'dirty' solucion and Sonar is saying 'God DONT DO THAT'.
More details to explain mysefl, my Header.jsp has that code:
<ul class="pull-right">
<c:if test="${empty sessionScope.USER}">
<li><spring:message code="header.manual"/><span><spring:message code="header.help"/></span></li>
</c:if>
If i build with Login.jsp i want that href linked to "url.com/home.public?help_login" and if is Register.jsp i want href liked to "url.com/home.public?help_register".
INFO: when i use register.jsp my url is "url.com/register.public" otherwise i use login.jsp my url is "url.com/login.public".
Sorry if my english is bad, but i think you will understand it.
EDIT: Layout.jsp
<body class="home" onload="nobackbutton();">
<c:choose>
<c:when test="${not empty sessionScope.ADMIN}">
<tiles:insertAttribute name="headerAdmin" />
<tiles:insertAttribute name="menuAdmin" />
</c:when >
<c:otherwise>
<tiles:insertAttribute name="header" />
<tiles:insertAttribute name="menu" />
</c:otherwise>
</c:choose>
<div class="container-fluid">
<tiles:insertAttribute name="miga" />
<tiles:insertAttribute name="body" />
</div>
</body>
Finally i used a document.ready (javascript) to identify de href and modify it and make sonar ignore that because i found nothing better than these. I don't want lose more time ...
Thnk Hooch for help
Can't you simply do something like the following?
<%
if (${sessionScope.USER} != null) {
%>
<li><spring:message code="header.manual"/><span><spring:message code="header.help"/></span></li>
<%
}else{
%>
<li><spring:message code="header.manual"/><span><spring:message code="header.help"/></span></li>
<%
}
%>
Edit: since you need to know the current page you could check
<%= request.getServletPath() %>
In the header.jsp

I am trying display image when condition is true.but i am getting error in <img> tage pls help me

<%
String optionselected =request.getParameter("digram");
out.print(optionselected);`
if(optionselected.equals("1"))
<img src="C:\Users\jitutjs\workspace\dispImpl\Traing.jpg"/>
else
if(optionselected.equals("2"))
<img src="C:\Users\jitutjs\workspace\dispImpl\WebContent\Circle.jpg"/>
else
out.print("no diagarm");
%>
<img src="..." /> is not related to java, it is a html tag, all that is inside the scriplets is treated as java code. If you want to use scriplets, you must do it like this:
<%
String optionselected =request.getParameter("digram");
out.print(optionselected);`
if(optionselected.equals("1"))
%>
<img src="C:\Users\jitutjs\workspace\dispImpl\Traing.jpg"/>
<%
else
if(optionselected.equals("2"))
%>
<img src="C:\Users\jitutjs\workspace\dispImpl\WebContent\Circle.jpg"/>
<%
else
out.print("no diagarm");
%>
P.S. My advice is to not use scriplets, they are old and deprecated. It is better to take a look at JSTL tag library.
img tag should be out of scriplest. Also, You have given the src value in wrong way, you can use absolute path of your web-app as <img src="/Circle.jpg"/> as this may vary when application is deployed elsewhere.

Can we redirect one jsp page to another jsp page

I want to open a jsp page without accessing my servlete code. i.e. I neither have to input my url in (action="url") my jsp code nor have to access my Servlete code.
<form id="main" method="post" name="main" action="dpRegPost" onsubmit="return validate();">
Can anyone help me in this?
You can add javascript to your jsp file
<script type="text/javascript">
window.location.href = "www.google.com";
</script>
or using jsp
<%
response.sendRedirect("www.google.com");
%>
You can also try this
<jsp:forward page = "abc.jsp" />
Use jstl taglibrary in your current jsp page.Make available the taglibrary using below code
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Use Following code in jsp to redirect
<c:redirect url="/xxx.jsp"/>
Try this:
<form id="main" method="post" name="main" action="" onsubmit="redirect(this);">
<input type="submit" name="submit"/>
</form>
function redirect(elem){
elem.setAttribute("action","somepage.jsp");
elem.submit();
}
You can also call page directly with:
<jsp:redirect page="xyz.jsp" />
use the following code to redirect on another page in js...
$('#abc_id').click(function() {
updateSubContent("abc.jsp");
});

Retrieving null value from servlet to jsp

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

Validation errors display of every command in page header (Spring 3)

I want to display a div with all error messages in my page header. I know how to do for for a single command eg:
<spring:hasBindErrors name="<my_command>">
<div class="error_box">
<c:forEach var="error" items="${errors.allErrors}">
<spring:message message="${error}"></spring:message><br />
</c:forEach>
</div>
</spring:hasBindErrors>
but I would like to have global div for every command So I wouldn't need to repeat nearly same code. Any help appreciated :)
The only idea I have is:
<%
Enumeration en = request.getAttributeNames();
while(en.hasMoreElements()) {
String attr = (String)en.nextElement();
if(attr.startsWith("org.springframework.validation.BindingResult")) {
%>
<spring:hasBindErrors name='<%= attr.substring(attr.lastIndexOf(".")+1) %>' >
<div class="error_box">
<c:forEach var="error" items="${errors.allErrors}">
<spring:message message="${error}"></spring:message>
<br />
</c:forEach>
</div>
</spring:hasBindErrors>
<%
}
}
%>
Maybe one day I will get a better answer :)

Categories