Add <error-page> to specifc servlet within web.xml - java

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.

Related

Why I get null when retrieving servlet's init parameter from JSP?

I have DD (web.xml file) with very simple code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>TestProject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>test</servlet-name>
<jsp-file>/result.jsp</jsp-file>
<init-param>
<param-name>email</param-name>
<param-value>example#gmail.com</param-value>
</init-param>
</servlet>
<context-param>
<param-name>name</param-name>
<param-value>Max</param-value>
</context-param>
</web-app>
Notice I have two parameters (one in application, other in configuration scope). When I try to get them inside result.jsp with:
<html><body>
Name is: <%=application.getInitParameter("name") %>
<br>
Email is: <%=config.getInitParameter("email") %>
</body></html>
, I get following output:
Name is: Max
Email is: null
My question is simple: how did I get NULL for "email" parameter? Shouldn't my JSP file "see" how I configured it and return "example#gmail.com"?
Is that your entire web.xml file? And by any chance are you accessing the JSP directly in the browser? Like:
http://localhost:8080/<yourAppContext>/result.jsp
If that is the case, then you will get this response:
Name is: Max
Email is: null
It is not wrong. It is correct.
The reason you get this result is that you are not accessing the JSP through the configuration you defined in web.xml, you are just accessing the JSP directly, which behind the scene has a different implicit configuration, and it's not the one you think you are configuring.
If you want this response:
Name is: Max
Email is: example#gmail.com
Then you need to add a servlet mapping. The complete configuration is:
<servlet>
<servlet-name>test</servlet-name>
<jsp-file>/result.jsp</jsp-file>
<init-param>
<param-name>email</param-name>
<param-value>example#gmail.com</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
and you need to access this URL, not the JSP path, with:
http://localhost:8080/<yourAppContext>/test
You might want to also read these:
Url Mapping For Jsp
Servlet JSP web.xml
To further drive the point home, it's important to mention that you need a mapping for one of your servlets to be useful. If you just define a servlet in web.xml, it just sits there. You need to tell the server how to use it, and for that you use the <servlet-mapping>. It's saying to the server that for a request on a path, some specific servlet needs to be called to handle the request.
You can create this mapping to point to a servlet class using <servlet-class> or to a JSP using <jsp-file>. They are basically the same thing, since a JSP eventually becomes a servlet class.
What I think is confusing you (based on the comment below) is that for JSP files you already have some implicit mapping created by the server, as described here.
When you access the JSP directly, with
http://localhost:8080/<yourAppContext>/result.jsp
you are using the implicit server mapping which contains no special configuration attached (like the email you want to send to it).
When you access the JSP with
http://localhost:8080/<yourAppContext>/test
you are accessing your mapping. And this you can configure however you want, and send it whatever parameters you want, and your JSP will now be able to read them.

web.xml: Request to url-pattern with wildcard timeouts

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?

AngularJS throwing errors root path

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>

RequestDispatcher: Servlet mapping doesn't work properly

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");

web.xml servlet mapping for wildcard requests

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>

Categories

Resources