in php, we can get the path after the index page, for example if the URL is http://server.com/index.php/resource/name, we can get the path 'resource' and 'name'. in jsp, it doesn't work. URL http://server.com/index.jsp/resource/name outputs '404 not found'. i'm thinking like ignoring the path after index page or welcome page maybe. any idea how to get these paths 'resource' and 'name' in jsp?
You shouldn't, JSP should contain your view, but no logic. It's simple to do what you want with a servlet, by using the servlet mapping. For example, assuming you have a servlet named index and this section in your web.xml
<servlet-mapping>
<servlet-name>index</servlet-name>
<url-pattern>/index/*</url-pattern>
</servlet-mapping>
This will match http://server.com/index/resource/name. There are several methods in the Request object your servlet will receive that allow you to see the different path elements, like getQueryString or getContextPath or getPathInfo.
Related
I have to get an image from an url finished in /jcaptcha.jpg
I've defined in the web.xml file the following nodes:
<servlet>
<servlet-name>jcaptcha</servlet-name>
<servlet-class>com.domain.portales.jcaptcha.servlet.ImageCaptchaServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jcaptcha</servlet-name>
<url-pattern>/jcaptcha.jpg</url-pattern>
</servlet-mapping>
In my jsp, I try to get the image, and I get it, but the code of doGet method of my servlet doesn´t execute. However, if I get the image using the following code
<p><img id="imgrecarga" src="${pageContext.request.contextPath}/jcaptcha.jpg"/></p>
that produces the value
/WCAC_FormularioContacto-portlet/jcaptcha.jpg
I obtain the image rigth and enter to the doGet() method.
It's strange because on that path doesn't exist the .jpg file.
I have to obtain the image by the first way entering on the doGet method, anyone knows what is happening? Have I to configure something on Liferay?
Finally the problem was because I was using a bad domain. For my local tests I've to access with an url starting by localhost:8080...
I am using Jetty. My default servlet is making a simple forward to an HTML file in my WEB-INF folder that is causing a java.lang.StackOverFlowError error. The error is fixed if I rename the file I am forwarding from a .html to .jsp
DefaultServlet.java
public class DefaultServlet extends HttpServlet{
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
req.getRequestDispatcher("WEB-INF/home.html").forward(req, resp);
}
}
web.xml
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>Default</servlet-name>
<servlet-class>DefaultServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
My guess is that instead of inserting the html content in the response body, the forward is sending the browser a redirect to /WEB-INF/home.html. This again calls the DefaultServlet and gets into an infinity loop. How can I prevent this?
Thanks.
The "default servlet", which is mapped on a special URL pattern of /, is a very special servlet which is invoked when there's a request which does not match any of the servlets mapped on a more specific URL pattern such as *.jsp, /foo/*, etc.
When you forward to home.html, for which apparently no one servlet is registered, then the default servlet is invoked once again. However, the default servlet is ignorantly forwarding to the very same HTML file once again instead of actually serving the requested HTML file. It'll on the forward still find no one servlet matching the forward URL and it'll still invoke the default servlet once again. And again. Etc. When this is performed so many times that the stack cannot keep track anymore of all those in sequence invoked doGet() methods (usually around 1000), then you'll get a StackOverflowError.
That it works with a JSP file has actually a very simple reason: there's already a JspServlet registered on an URL pattern of *.jsp. So the badly designed default servlet isn't invoked.
Your default servlet should instead be obtaining the HTML file's contents via ServletContext#getResourceAsStream() and write it to the HttpServletResponse#getOutputStream().
However, it's also quite possible that you completely misunderstood the whole meaning of "default servlet" and/or the special meaning of the URL pattern / and actually merely want a servlet acting as home page. In that case, you should be mapping the servlet on a more specific URL pattern (and please rename the currently obviously quite confusing class name DefaultServlet to something else):
<servlet>
<servlet-name>home</servlet-name>
<servlet-class>com.example.HomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>home</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
And then register exactly that URL as welcome file:
<welcome-file-list>
<welcome-file>home</welcome-file>
</welcome-file-list>
You need kind of exclude urls ends with "html".
See for example this link explaining similar problem solution Can I exclude some concrete urls from <url-pattern> inside <filter-mapping>?
I want to get a photo in my jsp pages. I implemented the servlet in this way (in doGet method):
{...
byte[] imageData = u.getFoto();
response.setContentType("image/jpg");
response.getOutputStream().write(imageData);
..}
where u is a User type.
My question is: how can I set the src path in my jsp page to retrieve the image from Servlet??
You would specify the mount point in your web.xml, with something like this:
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/img/myservlet</url-pattern>
</servlet-mapping>
That will take the servlet named MyServlet and mount it to /img/myservlet. Then, in your jsp you would just use an img tag pointing to the url-pattern specified above.
<img src="/img/myservlet" />
Note: if your webapp is not mounted to /, you will also need to specify the contextPath for the application in the path.
My web app directory structure is
myApp
-src
- filtersPackage(all my filters lie in this package)
- servletsPackage(all my controllers and models lie in this package)
- web-content
- web
- views(all my jsp files lie in this dir)
- js(all my javascript files lie in this dir)
In login.jsp, user clicks on FB login button, inside js/FBAUth.js I collect login details and I send it to my auth.do servlet using jquery ajax POST method.
The SessionFilter allows the request to go to AuthServlet's doPost method. In AuthServlet If the credentials are correct then do the following
url = "dashboard.jsp"
request.getSession().setAttribute("uid", id);
view = request.getRequestDispatcher(url);
view.forward(request, response);
return;
I have seen in debug mode that these lines execute but my browser is still stuck on login page. The values for view.requestURI -> /myApp/web/views/dashboard.jsp and
view.servletPath -> /web/views/dashboardTmp.jsp.
I also tried response.sendRedirect(url), but still the browser is stuck on login page. Nothing executes after these lines.
In web.xml, my auth servlet is mapped as follows
<servlet>
<servlet-name>Auth</servlet-name>
<servlet-class>servletsPackage.AuthServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Auth</servlet-name>
<url-pattern>/web/views/auth.do</url-pattern>
</servlet-mapping>
I also tried calling doGet inside doPost, and executed this code in doGet, but all in vain.
What am I missing here ?
This approach seems to be a bit flawed. Your forward is not working because you are using AJAX to post data. You would need to use javascript or rather jquery to handle the redirection.
Check if your jquery AJAX function's callback method is working. print out the responseText using alert(responseText);
Check this link for more information about your problem. And check the first answer. It provides a solution for this problem.
Try to return something from your servlet (write in response writer), retrieve that as ajax response, depending upon that, do a window.location.href ="YOUR URL";
I want to create a JSP page or servlet that will work in 2 ways.
A user visits their own profile page:
http//something.com/profile/
Or they visit their friends page:
http://something.com/profile/FriendsName
They could also visit their own page through an explicit URL like:
http://something.com/profile/YourName
I have a servlet-mapping setup as follows to map any requests to /profile to my JSP that will handle that request.
<servlet>
<servlet-name>Profile</servlet-name>
<jsp-file>/profile.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>Profile</servlet-name>
<url-pattern>/profile</url-pattern>
</servlet-mapping>
Then I was thinking I could setup a filter that will parse the HTTPServletRequest's URL to read after the /profile/.
<filter>
<filter-name>profile-filter</filter-name>
<filter-class>ProfileFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>profile-filter</filter-name>
<url-pattern>/profile*</url-pattern>
</filter-mapping>
Now the filter could set attributes in the HttpServletRequest, how would i go about pulling those out in the JSP page to check if a user name was specified and check if your own name was set?
How would I go about creating a page scoped bean in the ServletFilter so I could use in the JSP page using jspBean like this:
<jsp:useBean id="profileInfo" scope="page" class="ProfileInfo" />
I think a filter only serves to artificially break the logic apart. You can very easily determine the value of the portion of the URL that doesn't match the url-pattern described in the web.xml. The HttpServletRequest class has a method that returns this value for you, it's called getPathInfo(). Here's an example:
<%
String path = request.getPathInfo();
if (path == null || "".equalsIgnoreCase(path)) {
// The path was empty, display the current user's profile
} else {
// Display the named profile
}
%>
This doesn't help you at all with the request beans, but I think it helps with the design.
I'm not completely following the question, but if you want a jsp to access request parameters or attributes, just do:
<%
String parameter = request.getParameter("parameter");
String attribute = request.getAttribute("attribute");
%>
You can also get access to the request url, etc... if you need to do anything with those.
Personally, I'd recommend that you use a servlet to handle your requests, and forward to the jsp of your choosing (possibly after setting session information that the jsp can use).
Using server level filters and request attributes for this kind of thing may be a bit overkill, but only you know your project's true requirements.