I want to do something like this:
localhost:7001/servlet/character?name=zombies
I tried doing this:
<servlet-mapping>
<servlet-name>zombies</servlet-name>
<url-pattern>/character?name=zombies</url-pattern>
</servlet-mapping>
but it doesn't work and giving me not found error. Any advice or solution on how to do it?
The ?name=zombies portion of your url-pattern should not be used in the web.xml. It is a query parameter that is not actually a part of the servlet mount point. You would need to access the variable name in your zombies servlet via request.getParameter("name").
you are trying to append query string the stuff followed by ? with your URL pattern.
URL pattern is meant to map your servlet class. if you can pass query string in the address bar itself.
If you want to pass a parameter to your servlet then do like this
<servlet>
<servlet-name>zombies</servlet-name>
<servlet-class>com.ZombiesDemo</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>zombies</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>zombies</servlet-name>
<url-pattern>/character</url-pattern>
</servlet-mapping>
This you can retrive in ZombiesDemo.java servlet as
public void init(ServletConfig servletConfig) throws ServletException{
String name = servletConfig.getInitParameter("name");
}
Related
In our app for all the notification we trigger through mail.
All the templates have non sso link
>/Userlogin?param1=param2value¶m2=param2value">Link to access app
I need to modify this link in all templates to
>/Userloginsso?param1=param2value¶m2=param2value">Link to access app
Since there are many templates and takes lot of manual effort, is there any way we can redirect the request of Userlogin to Userloginsso. Any configuration that we can do in web.xml ?
Considering you have a mapping for Userlogin in web.xml as below:
<web-app>
<servlet>
<servlet-name>Userlogin</servlet-name>
<servlet-path>com.something.Userlogin</servlet-path>
</servlet>
<servlet-mapping>
<servlet-name>Userlogin</servlet-name>
<url-pattern>/Userlogin</url-pattern>
</servlet-mapping>
</web-app>
Modify existing mapping to :
<web-app>
<servlet>
<servlet-name>Userloginsso</servlet-name>
<servlet-path>com.something.Userloginsso</servlet-path>
</servlet>
<servlet-mapping>
<servlet-name>Userloginsso</servlet-name>
<url-pattern>/Userlogin</url-pattern>
</servlet-mapping>
</web-app>
Now all calls to Userlogin will be redirected to Userloginsso servlet.
You could do a simple redirect in your UserLogin servlet with the following:
public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException {
String param1 = request.getParameter ("param1");
String param2 = request.getParameter ("param2");
// other parameters
// Build the new url: if too much parameters, prefer using a StringBuilder over String concatenation for better performances
String baseUrl = request.getContextPath () + "/Userloginsso?param1=" + param1 + "¶m2=" + param2;
String encodedUrl = response.encodeRedirectURL (baseUrl);
response.sendRedirect (encodedUrl);
}
If I understand your question correctly you could use a filter example here get the url and foward it somplace else in your app. Or and url rewrite library such us this one
If you still want a servlet you could use a ProxyServlet. There are already many good implementations.
Examples:
Complex proxy servlet with all features
Simple proxy servlet, limited features
I would like to use Cutome servlet only for url which are not having extensions like .jsp,jss,css and image extensions.
I tried like this but no use.
Web.xml :
<filter>
<filter-name>ControllerFilter</filter-name>
<filter-class>tut.controller.ControllerFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ControllerFilter</filter-name>
<servlet-name>ControllerServlet</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>tut.controller.ControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>FileServlet</servlet-name>
<servlet-class>tut.controller.FileServlet</servlet-class>
</servlet>
Filter :
String requestedUri = ((HttpServletRequest)request).getRequestURI();
System.out.println("requestedUri:"+requestedUri);
if(requestedUri.matches(".*[css|jpg|png|gif|js|jsp]*")){
//How to configure the default calling here
return;
}
else
{
// ControllerServlet for other requests
chain.doFilter(request, response);
}
Try with $ that represents The end of a line in regex pattern
requestedUri.matches(".*[css|jpg|png|gif|js|jsp]$")
If matched then follow the chain otherwise forward the request to the required Servlet, JSP or HTML.
if (uri.matches(".*[css|jpg|png|gif|js|jsp]$")) {
filterChain.doFilter(req, res);
}else{
// forward the request to Servlet/JSP/HTML
req.getRequestDispatcher("path").forward(req, resp);
}
web.xml:
use / as url pattern for filter to inspect all the request then based on uri forward it to Servlet/JSP/HTML in the filter itself.
<filter-mapping>
<filter-name>ControllerFilter</filter-name>
<servlet-name>/</servlet-name>
</filter-mapping>
Find a better solution here Servlet for serving static content
I don't think that Filter is a good mechanism for achieving what you want. If Filter gets a request for unwanted extensions, it's too late to deal with this.
You can modify your request and response in Filter to do pre-processing or post-processing, but it's probably not what you're looking for. You want those extensions not to be routed to the servlet at all, but rather be processed by a static content handler.
To do what you want you'll need to put your static content to a folder which is not under path that can match Servlet path, otherwise your servlet is going to get requests for the static content at which point you'll need to do something about those requests inside your servlet.
<servlet-mapping>
<servlet-name>JAX-RS REST Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
for my
<servlet>
<display-name>JAX-RS REST Servlet</display-name>
<servlet-name>JAX-RS REST Servlet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
Servlet i.e the entry point of my app. In many examples I see everyone gives some path in the URL pattern but in my case I am just using /*. Is this ok? Or using some path in it has some benefits? Like faster URL matching? i.e the request if for the web service or so?
Firstly, it is not incorrect to have a /*.
If you have only one listener processing all incoming requests then what you have is absolutely fine. If you have multiple listeners/services processing different url patterns then of course, you will have different mappings for different url patterns.
I prefer to add a full url pattern like /path1/xyz/* if i know the pattern instead of /* so you i dont need to modify this mapping when i add another mapping/listener to process a different url pattern.
Application configuration:
Web application using java first method of creating JAX-WS 2.0 Web Services with annotations.
WebLogic 10.3
My Requirements
The requirements I have are to deploy a single web service implementation class, but change logic based on the URL from which the service was accessed.
Question:
I'm assuming a good way to do this is to deploy different mappings in web.xml and initialize them with different parameters. Is there a better way?
What is the best way to switch logic off the URL from which the web service was accessed? Should I try to configure two servlet mappings in web.xml with initialization parameters (tried, but couldn't get it to work), or should I parse the URL in the service impl? Any other alternatives?
What I've Tried (but didn't work)
I have tried adding the <init-param> in the <servlet> element in web.xml. However, can't get to the ServletConfig object inside the web service to retrieve the param. The web service does not have all the functionality of a standard Servlet (even if I implement Servlet or ServletContextListener). I only have access to the WebServiceContext (it seems) and from there I can only get <context-param> elements--but I would need <init-param> elements instead.
In web.xml, I enter two <servlet> elements using the same Java class, but which map to two different URLs as follows. Notice how the "source" param is different in each Servlet mapping.
<servlet>
<servlet-name>Foo</servlet-name>
<servlet-class>com.Foo</servlet-class>
<init-param>
<param-name>source</param-name>
<param-value>1</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Foo</servlet-name>
<url-pattern>/Foo</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Bar</servlet-name>
<servlet-class>com.Foo</servlet-class>
<init-param>
<param-name>source</param-name>
<param-value>2</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Bar</servlet-name>
<url-pattern>/Bar</url-pattern>
</servlet-mapping>
You very well may have, but did you try using MessageContext at runtime to determine what the source is?
#WebService
public class CalculatorService implements Calculator
{
#Resource
private WebServiceContext context;
#WebMethod
public void getCounter()
{
MessageContext mc = wsContext.getMessageContext();
// you can grab the HttpSession
HttpSession session = (HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST)).getSession();
// ...or maybe the path info is enough
String path = mc.get(MessageContext.PATH_INFO);
// the query itself should almost definitely be enough
String query = (String) mc.get(MessageContext.QUERY_STRING);
}
}
I got the idea from http://sirinsevinc.wordpress.com/category/jaxws/. Haven't tried it, though.
http://127.0.0.1:8888/socialnetwork/contactsService
That's the current URL for one of my servlets. My question is, how do I change it?
In my web.xml file, altering
<servlet-mapping>
<servlet-name>contactsServiceServlet</servlet-name>
<url-pattern>/socialnetwork/contactsService</url-pattern>
</servlet-mapping>
to
<servlet-mapping>
<servlet-name>contactsServiceServlet</servlet-name>
<url-pattern>/a/contactsService</url-pattern>
</servlet-mapping>
Makes absolutely NO difference to the URL it requests when I make an RPC-call to the servlet.
Once you have done the above you need to change where you invoke (Which is described in the Annotation below) as in...
// The RemoteServiceRelativePath annotation automatically calls setServiceEntryPoint()
#RemoteServiceRelativePath("email")
public interface MyEmailService extends RemoteService {
void emptyMyInbox(String username, String password);
}
See http://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/gwt/user/client/rpc/RemoteServiceRelativePath.html