I have an index.jsp page which uploads an image. On submit it goes to a servlet Upload.java. In the servlet I am checking if the extension in of image("jpg","png",etc) and forwards to new jsp page else it shows an error message and includes the same index.jsp page.
My servlet is a package named "servlets".
If I select an image then it is working properly. But if I select any file other than image then it shows the error with the index.jsp page as intended. Till now it works fine but if I upload any file even image from here, the server complains.
Here is how I am including the index.jsp page in UploadServlet.java servlet.
out.println("This type of file is not allowed. Please select an image.");
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp");
dispatcher.include(request, response);
Here is the error from the server when I try to upload the image second time.
HTTP Status 404 - /UploadImage/servlets/servlets/UploadServlet
type Status report
message /UploadImage/servlets/servlets/UploadServlet
description The requested resource (/CropImage/servlets/servlets/UploadServlet) is not available.
Apache Tomcat/6.0.13
It is appending the servlet's package name to the url.
How to solve this problem?
Apparently you're using a relative action URL in your <form>.
<form action="servlets/UploadServlet" ...>
When you open index.jsp, the request URL is
http://localhost:8080/UploadImage/index.jsp
When you submit the form, the action URL is relative to the current folder, so request URL will be
http://localhost:8080/UploadImage/servlets/UploadServlet
When you submit the form once again, the will be still relative to current folder, so you end up in
http://localhost:8080/UploadImage/servlets/servlets/UploadServlet
You need to fix it to be a domain-relative URL, starting with a leading slash.
<form action="/UploadImage/servlets/UploadServlet" ...>
This way the URL will be resolved relative to the domain root. You can also resolve the context path dynamically by ${pageContext.request.contextPath}:
<form action="${pageContext.request.contextPath}/servlets/UploadServlet" ...>
Your url is wrong. You can open the web.xml and find the "servlet-mapping" element there you can find the mapping url.
I guess your url may be "/CropImage/servlets/UploadServlet" .you can try to delete one "servlets" in the url.
Related
I have a JSP file under the folder name adminlogin. I am using getRequestDispatcher and forword to call the jsp file but I am unable to get the request and it is showing me 404 error.
The url link is http://localhost:8084/demo/adminlogin/menu.jsp
Following is my code,
String opt = menu_display.print_opt();
request.setAttribute("menu_display", opt);
request.getRequestDispatcher("adminlogin/menu.jsp?p=1").forward(request, response);
The call to adminlogin should have a "/" since it is relative to the context path of your application:
request.getRequestDispatcher("/adminlogin/menu.jsp?p=1").forward(request, response);
If you do not have a "/" the servlet engine is looking for it relative to the current url.
I use Apache Tiles to unite multiple jsp pages.What I want is to get the URI of the request that came from web client (from browser). However, when in my jsp page I use
${pageContext.request.requestURI}
I get not web client uri but the local path of the jsp file. For example when web user enters http://company.com/something/ (I want to get /something/) I get /jsp/articles/index.jsp.
I tried requestScope.request.requestURI but it returns empty string. How can I get web client request URI
As per your question,you said when you enter "http://company.com/something/" in the browser,you get /jsp/articles/index.jsp in JSP,it seems your original request has been forwarded to new one. You can try below to get the orginal URI in JSP page.
<% String originalUri = (String) request.getAttribute("javax.servlet.forward.request_uri"); %>
i have been trying to fix for days, but no fix yet, i want to have fix base url with root context , rest of the url string will change, but my base url should be fixed for each request.
here is scenario,
in a home page, when user clicks "Login" it will call menu controller and the request url will be as below
http://localhost:8080/myApp/menu/login.jsp
once my login page loads, when i do "Sing in", the url should be as below
http://localhost:8080/myApp/user/singIn.jsp
but above was not working and my url request is some thing like
http://localhost:8080/myApp/menu/user/singIn.jsp
so it was taking relative path instead of absolute, i have code below code added to my layout jsp to fix this problem to have base url fixed, but it is not working.
<base href="${pageContext.servletContext.contextPath}">
above code i have added to layout.jsp which contains header, body and footer, and my request presents in body jsp.
Edit :- the request are jquery ajax request
use <base href="${pageContext.contextPath}">
instead of
<base href="${pageContext.servletContext.contextPath}">
`<%String path = request.getContextPath();
String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
`
<base href="<%=basePath%>">
I think the title above is a bit confusing.
What I'm trying to achieve:
I have a jps page(located in WEB-INF) with a hyperlink in it that will call another jsp (in WEB-INF) via servlet.
I understand that this can be achieved using the following:
Go to this page
But because there will be lots of hyperlinks, my idea was to have a general servlet(OpenPagesServlet) to handle all those pages.
Something like this:
JSP page:
<% request.setAttribute("page", "page1.jsp");%>
Page 1
OpenPagesServlet in doGet method:
String page = (String) request.getAttribute("page");
request.getRequestDispatcher("/WEB-INF/" + page).forward(request, response);
I tried the code above and I get:
HTTP Status 404 - Not Found
type Status report
messageNot Found
descriptionThe requested resource is not available.
But if I try with session.setAttribute / sesion.getAttribute the code works fine, but I don't want to have sessions on each time I click on hyperlinks.
The other approach I found was to use:
Page 1
and inside the servlet:
String page = (String)request.getParameter("value");
request.getRequestDispatcher("/WEB-INF/" + page).forward(request, response);
It worked, but this approach is not good because the page can then be accessed directly using the url:
http://localhost:8080/WebApp/OpenPagesServlet?value=page1
So...my question is why request.setAttribute/request.getAttribute is returning 404?
Is there a different approach to achieve what I'm trying to do?
An HttpServletRequest and its attributes only live for the duration of one HTTP request/response cycle. After yo've set the attribute in the JSP, the JSP is rendered and sent as part of the HTTP response body. The Servlet container considers the request handled and clears its attributes. The attribute is now gone.
It is therefore no longer available in the next request that arrives after the user clicks the link.
The session attribute or request parameter is fine. Consider looking into the Front Controller pattern.
Also, consider using the core tag library (in particular the url tag) instead of scriptlets for constructing your links.
Goal: User tries to access page without authentication. Site redirects to login page, when they enter details they are returned to the page they were trying to access.
I have a filter which records the last url the user was at in the session. The following code is how i get the uri.
String uri = request.getRequestURI().toString();
String queryString = request.getQueryString();
String completeUri = uri;
if (queryString != null)
{
completeUri += "?" + queryString;
}
In practice this filter seams to be catching external css files, individual images on a page etc so about half the time it works and half the time its pointing to an image or css file.
The mapping for the filter is...
<filter>
<filter-name>ComprehensiveFilter</filter-name>
<filter-class>core.website.control.filter.ComprehensiveFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ComprehensiveFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
So question is. Why does my code store images and external files instead of the url the user was just at? I assume it has something to do with my mapping of the filter.
You should use this code only in case you detect unauthorized access and redirect to the login page. And you should pass it as param, like response.sendRedirect("login.jsp?" + completeUri)
Now, if the filter is applied to every resource (as you do), it will be triggered for images and css files that are included in the login.jsp. You must exclude the login.jsp itself from this redirection (otherwise you will enter a loop), and you must also exclude the css files. This depends on your URL scheme.
if all your pages are .jsp, then map the filter to *.jsp
if your actions to through a single servlet (like a dispatcher servlet), then map the filter to that servlet (instead of <uri-pattern> set <servlet-name>)
if you have "pretty urls", then (in the filter) check if the requested resource ends with .css, .png, .gif etc, and don't enter the redirection logic.
When you have any images on the page, meaning an html <img> tag, then the tag's src attribute refers to an actual URL (see http://www.w3schools.com/tags/tag_img.asp). So when the page is loaded, the browser sends out another request to load that image. Your filter also catches not only the request for the entire page, but also those image loading requests.