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>
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?
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.
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");
Hello i have a question about servlet calling another servlet
I have a main servlet called Relay which is going to be responsible to control the other servlets
the user will click on and will be forwarded to Relay servlet
<li>Check the available animals </li>
inside Relay servlet will get the value of the parameter to determine which servlet is going to run
String selectAnimal = request.getParameter("selectAnimal");
if (selectAnimal.equals("SelectAnimalServlet")){
getServletContext().getNamedDispatcher("/SelectAnimalServlet")
.forward(request, response);
//for testing
System.out.println("Request forwarded to " + selectAnimal + " servlet");
}
SelectAnimalServlet code:
try
{
HttpSession session = request.getSession(true);
session.getAttribute("currentSessionUser");
List<AnimalSelectBean> beans = DAO.getAnimalList();
request.setAttribute("beans", beans);
request.getRequestDispatcher("CheckAnimal.jsp").forward(request, response);
}
Now when i run that it's not working for some reason, if i change the link to SelectAnimalServlet directly the code works any idea how to solve this ?
Edit:
Here is my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>content.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>UpdateAnimalServlet</servlet-name>
<servlet-class>content.UpdateAnimalServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>SelectAnimalServlet</servlet-name>
<servlet-class>content.SelectAnimalServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>Relay</servlet-name>
<servlet-class>content.Relay</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SelectAnimalServlet</servlet-name>
<url-pattern>/SelectAnimalServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UpdateAnimalServlet</servlet-name>
<url-pattern>/UpdateAnimalServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Relay</servlet-name>
<url-pattern>/Relay</url-pattern>
</servlet-mapping>
</web-app>
also I changed:
getServletContext().getNamedDispatcher("/SelectAnimalServlet")
.forward(request, response);
to:
response.sendRedirect(response.encodeRedirectURL(selectAnimal));
and still the same thing blank webpage with http://localhost:8080/oosd/Relay?selectAnimal=SelectAnimalServlet link
getNamedDispatcher expects a servlet name; you're providing it with a servlet URL.
Either use the name, or use getRequestDispatcher with the URL.
Since you're forwarding, the URL will not change--there is no redirect response sent back to the browser on a forward. The contents of the forward are written directly to the original response.
Now that you're forwarding, you need to redirect to the URL, not just the name of the servlet.
What does the servlet you redirect to do for output?
I don't believe your parameter naming convention makes any sense. The parameter shouldn't be named the same as a servlet name; the parameter should be something like "command" or "select". You would then use the command parameter value to look up the URL of the servlet. Or, in your case, just prepend a /, and you're done. There's no need to do any if/else comparisons.
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>