url pattern for html file in web.xml - java

we know how to set url pattern for servlet but I am unable to set url pattern for html in web.xml, can u help me to find solution, I googled but, can't able to get it, please find below for my problem.
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>auth.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
in above code I am setting url pattern for **Login** servlet class in web.xml, like wise can I able to set url pattern for html file in web.xml
pls help to find solution thank you in advance

If you want to protect *.html files from direct access (by placing *.html files under WEB-INF) you can use a Servlet which would be only responsible for forwarding all such requests to intended html files.
<servlet>
<servlet-name>HTMLServlet</servlet-name>
<servlet-class>my.package.HTMLServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HTMLServlet</servlet-name>
<url-pattern>/somepath/*.html</url-pattern>
</servlet-mapping>
Code in servlet class may look like this
...
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String requestedPath = //... code for getting requested HTML path
request.getRequestDispatcher(requestedPath).forward(request, response);
}
...

If you don't mind to change your HTML page to JSP you can set url pattern for it like this:
<servlet>
<servlet-name>Error</servlet-name>
<jsp-file>/pages/error.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>Error</servlet-name>
<url-pattern>/error</url-pattern>
</servlet-mapping>

URL pattern are for servlet and filters.
For servlet
<servlet-mapping>
<servlet-name>Servlet-name</servlet-name>
<url-pattern>/< Pattern ></url-pattern>
</servlet-mapping>
For Filter
<filter-mapping>
<filter-name>Filter-Name</filter-name>
<url-pattern>/< Pattern ></url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
Those are not for Html file.
Infact there are no pattern configuration for JSPs too.

Related

Mapping .html url-pattern in struts 1

I would like to capture the .html url in my actionServlet.
Currently .do url I am capturing in actionSrvlet.
Web.xml -- ActionServlet `
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>com.mm.wp.webapp.common.WPActionServlet</servlet-class>
<init-param>
<param-name>labelresource</param-name>
<param-value>LabelResources</param-value>
</init-param>
<init-param>
<param-name>errorresource</param-name>
<param-value>ErrorResources</param-value>
</init-param>
<init-param>
<param-name>tootipresource</param-name>
<param-value>TooltipResources</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>`
Could you please suggest me what i have to do more to capture .html url in my ActionServlet ?
that what i am not getting i am assuming as .do url capturing in action servlet same like that .html will work , but it does't.
Well, the Struts 1 flow would have the Controller Servlet expecting an Action Mapping (for that url minus the extension), an Action class and invoke the execute() method.
What would you do for your html path? You need to write a common custom Action class that reads the input request path and forwards to that html.
Something like (untested)
//Refer http://stackoverflow.com/questions/4278083/how-to-get-request-uri-without-context-path to get the url path
public class HtmlAction extends Action{
public ActionMapping execute(.....){
String reqHtml = //see above;
request.getServletContext().getRequestDispatcher(reqHtml).forward(request, response);
return null;
}
}

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?

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

Relay servlet (servlet calls another servlet)

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.

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