Its so confusing. Don't have any kind of idea what happend here:
I want to deploy a simple WAR-project. Two HttpServlets, one just forwards the request to another one:
...
String[] selectedOptionsLabels = ...
req.setAttribute("checkedLabels", selectedOptionsLabels);
try {
req.getRequestDispatcher("/confirmationservlet.do").forward(req, resp);
}
...
When I try to set some values on the form it works great without dispatcher, but when I try this example, my browser can't handle the servlet. It tries to download the file confirmationservlet.do. Confusing.
There seems to be a mapping problem, but I can't figure it out, since the deployment does also work fine.
Do you have an idea?
This is my web.xml (without outer web-app-tag) <--- Only for testing purposes, knowing there are annotations.
<servlet>
<servlet-name>FormHandlerServlet</servlet-name>
<servlet-class>
de.lancom.formhandling.FormHandlerServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FormHandlerServlet</servlet-name>
<url-pattern>/formhandlerservlet.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ConfirmationServlet</servlet-name>
<servlet-class>
de.lancom.formhandling.ConfirmationServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ConfirmationServlet</servlet-name>
<url-pattern>/confirmationservlet.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>dataentry.html</welcome-file>
</welcome-file-list>
Try the following method:
HttpServletResponse#sendRedirect()
to send a redirect.
response.sendRedirect("/confirmationservlet.do");
Related
I am making a "blog-platform" where users can write articles, and I want each article to get its own URL. Therefore, I'd like every page with the URL /blog/* to be sent to the same Servlet.
I have tried using web.xml like this:
<servlet>
<servlet-name>BlogServlet</servlet-name>
<servlet-class>package.BlogServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BlogServlet</servlet-name>
<url-pattern>/blog/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>MainServlet</welcome-file>
</welcome-file-list>
This doesn't work. /blog works, but /blog/ and /blog/any_string only gives off a timeout after a while. What am I doing wrong? Is there any other way I could implement this, other than parameters?
Im deploying a webapplication to tomcat 8 (renaming to ROOT.war) because the url pattern was set to / I thought that all requests would get directed the servlet. But that wasnt the case, eventually i realized that if I was starting the url with a ? such as
http://localhost:8080/?search=fred
it would not work, but without the ? it would work
http://localhost:8080/search=fred
Why is this ?
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
<display-name>Widget</display-name>
<servlet>
<servlet-name>WidgetServlet</servlet-name>
<servlet-class>com.jthink.WidgetServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>WidgetServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
If you want your Servlet to serve all URLs then url-pattern should be like this
<url-pattern>/*</url-pattern>
As the name suggests, it should be RegEx pattern. When you say / - it means to look for single occurrence of / in URL. But when you have multiple slashes in URL, something like
http://stackoverflow.com/questions/28945202/
then it should be /* which means you're asking it to look for zero or more occurrences of slash.
Hope that makes it clear :)
The servlet mapping is specific. You need to add a wildcard.
<servlet-mapping>
<servlet-name>WidgetServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Currently my web.xml file includes two servlets:
<servlet>
<servlet-name>mvc-servlet</servlet-name>
...
</servlet>
<servlet>
<servlet-name>api</servlet-name>
...
</servlet>
If I just add "error-page" in the current web.xml it will handle the error for both "api" and "mvc-servlet", and I don't want this to happen. I want it to be applied ONLY to "mvc-servlet", not "api".
Is there any way to achieve this?
You can catch servlet exception and throw new exception(i.e ApiServletException), then you will be able to add error page only for this exception.
I am having trouble finding why my application is throwing errors at one path but not the other.
My angular application functions fine at the following URL with my default servlet.
http://localhost:8080/#/
web.xml
<servlet-mapping>
<servlet-name>cr</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
I would expect with the below servlet mapping that I should be able to access another application with the following url.
http://localhost:8080/admin/
web.xml
<servlet>
<servlet-name>admin</servlet-name>
<jsp-file>/resources/admin/index.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>admin</servlet-name>
<url-pattern>/admin/*</url-pattern>
</servlet-mapping>
However it throws the following errors constantly until I pause the page in my chrome debugger.
SyntaxError: Unexpected token <
The admin application however does function if I go to the following URL.
http://localhost:8080/admin#/
For reference here is my app.js
'use strict';
var AdminApp = angular.module('AdminApp', ['AdminApp.services'])
.config(['$routeProvider', function ($routeProvider) {
// default route
$routeProvider.when('/', {templateUrl: 'resources/admin/views/dashboard.jsp', controller: 'DashboardCtrl'}).otherwise({redirectTo: '/'});
}]);
main.js
//Dashboard Controller
AdminApp.controller('DashboardCtrl', function ($scope) {
});
service.js
var service = angular.module("AdminApp.services", ['ngResource']);
Note: I know that the app, main, and service are not actually doing anything, I am just trying to get the shell working before I start extending it with valid data.
Here is the order in which I initiate everything.
After I changed my web.xml to below it functioned as expected. I still do not know why it was throwing errors.
<servlet>
<servlet-name>admin</servlet-name>
<jsp-file>/resources/admin/index.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>admin</servlet-name>
<url-pattern>/admin/</url-pattern>
</servlet-mapping>
I want one of my servlets (test2) to handles the "/" request (i.e. http://localhost/), while another servlet (test1) handles all other requests ("/*").
I set up my web.xml below, but the problem is that ALL requests go to test1.jsp (even the "/" request)
Can someone tell my how to accomplish this?
<servlet>
<servlet-name>test1</servlet-name>
<jsp-file>/test1.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>test1</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>test2</servlet-name>
<jsp-file>/test2.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>test2</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
----EDIT-----
i realized my question was a bit unclear and incomplete. here is an example of exactly what i want to accomplish..
http://mytestsite.com/ -> maps to http://mytestsite.com/index.html
http://mytestsite.com/servlet1 -> runs com.mytestsite.servlet1
http://mytestsite.com/* -> maps to http://mytestsite.com/catchall.jsp (i want all other requests that aren't mapped in web.xml to map to catchall.jsp)
so my web.xml looks as follows:
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>com.mytestsite.servlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>catchall</servlet-name>
<jsp-file>/catchall.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>catchall</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
so i noticed a strange problem. when i request http://mytestsite.com/, it goes to catchall.jsp before being redirected to index.html. however, it happens so quickly i wouldn't have even noticed it hitting catchall.jsp (but i put a System.out.println in this file, and it was definitely hitting it).
I think your goal is a bit confusing and brittle. However, to answer your question, try a welcome file entry for the http://your-domain.com/ request.
<welcome-file-list>
<welcome-file>/test2.jsp</welcome-file>
</welcome-file-list>
It is most common to then have test2.jsp perform a redirect or forward to some other 'controller' in your application. That way your MVC is always fired even on http://your-domain.com/ requests.
If you agree with me on that, then your welcome file should be index.jsp (to follow common conventions). The code in index.jsp is then a one-liner redirect to a 'welcome' servlet.
Use a forwarding filter instead of servlet. It's very simple to intercept "/" using such method.
filter --> /*
servlet1 --> /_some_hidden_path_1_
servlet2 --> /_some_hidden_path_2_
Really not sure about that, but maybe the order that you declare\map your servlets defines precedence. Try to declare\map test2 first and see.
Kind Regards
Try not mapping the / request to anything (get rid of the test2 servlet), and instead use a welcome file:
<welcome-file-list>
<welcome-file>
test2.jsp
</welcome-file>
</welcome-file-list>