I have a GWT Server which will connect o another server via RMI or CustomConnection. I would like to have a way of telling the GWT Server which connection type to use.
What I could think of :
Adding custom tag in web.xml
Create a normal file containing the value of the connection
Which one is better? And do you know any other optimal way?
If you know at startup time, I would go with a initialization parameter:
http://docs.oracle.com/cd/E11035_01/wls100/webapp/progservlet.html#wp159396
For example, the following entries in the Java EE standard Web Application deployment descriptor, web.xml, define two initialization parameters: greeting, which has a value of Welcome and person, which has a value of WebLogic Developer.
<servlet>
...
<init-param>>
<description>The salutation</description>
<param-name>greeting</param-name>
<param-value>Welcome</param-value>
</init-param>
<init-param>
<description>name</description>
<param-name>person</param-name>
<param-value>WebLogic Developer</param-value>
</init-param>
</servlet>
To retrieve initialization parameters, call the getInitParameter(String name) method from the parent javax.servlet.GenericServlet class. When passed the name of the parameter, this method returns the parameter’s value as a String.
Overriding the init() Method
You can have your servlet execute tasks at initialization time by overriding the init() method. The following code fragment reads the tags that define a greeting and a name in the J2EE standard Web Application deployment descriptor, web.xml:
String defaultGreeting;
String defaultName;
public void init(ServletConfig config)
throws ServletException {
if ((defaultGreeting = getInitParameter("greeting")) == null)
defaultGreeting = "Hello";
if ((defaultName = getInitParameter("person")) == null)
defaultName = "World";
}
Related
Is it possible to use static variables in my project to store data for all Servlets (they are in one .war file) and different requests? (It's not data that belongs to a distinct session)
data for all Servlets
You can use ServletContext for this.
Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file.
There is one context per "web application" per Java Virtual Machine. (A "web application" is a collection of servlets and content installed under a specific subset of the server's URL namespace such as /catalog and possibly installed via a .war file.)
For example: in web.xml
<context-param>
<param-name>param</param-name>
<param-value>Myname is web xml</param-value>
</context-param>
In your servlet
public class ParameterServlet extends HttpServlet {
---
public void init(ServletConfig config) throws ServletException {
super.init(config);
ServletContext context = getServletContext();
name= context.getInitParameter("param");
}
A complete example here.
For Objects
setting
getServletContext().setAttribute("myObj", obj);
getting
MyObj attribute = (MyObj)getServletContext().getAttribute("myObj");
you can access those objects across servlets.
Yes you can do that.
However it is better to define these constants in your web.xml using the <context-param> tag.
Servlets can then retrieve constants defined using the <context-param> tag using the call:
context.getInitParameter()
Example of name-value pairs in web.xml:
<context-param>
<param-name>name</param-name>
<param-value>Joe</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>password</param-value>
</context-param>
in web.xml you can define
<context-param>
<description>My variable</description>
<param-name>variable.name</param-name>
<param-value>value</param-value>
</context-param>
And then access it from Servlet code:
String variable = getServletContext().getInitParameter("variable.name");
Yes you can, the static variable will be accessible for all the servlet threads. But about using the static variable, you should make a proper decision depending on the factors like the life time of the data you want to store and the amount of data you are going to store.
And since it is used in servlets context, make sure that its thread-safe.
I have a pretty standard Spring 3.0.7 web app
The structure is like this
WebContent/
resources/
myStaticConent/
WEB-INF/
views/
myProtectedContent/
I am using the <mvc:resources> configuration for the static content and my controllers get views using the InternalViewResolver from WEB-INF/views
Now I have a requirement to return non-JSP content ( JPGs,PNGs,HTML,etc ) from a protected directory in WEB-INF
So a user might enter a URL like http:myWebApp/myProtectedContent and hit my protected content controller.
#Controller
public class HelloWorldController {
#RequestMapping(value="/myProtectedContent")
public String index() {
return "myjpg.jpg";
}
}
Essentially I want to conditionally serve a file just like I would a view. Anyone know how this can be done ?
I looked at some of the other methods here, Streaming using Inputstream seems overkill for files that are essentially static. Can I register another "view" type ? I need this to appear l( from the web browser side ) like a standard http request response ( like the current view implementation).
I would really like to avoid inventing my own file handling methods unless there is some reason why using the file access methods are better then Springs "other" view resolvers like ResourceBundleResolver
So the requirement is
Conditionally respond to a http request with variable file type (jpg,png,html) from inside WEB-INF without wrapping in a jsp or having the file interpreted by the JSTL view. The names of the files are known and static. The controller will determine the file name based on its own business logic.
You can reproduce the behavior of the underlying implementation of <mvc:resources/> which is org.springframework.web.servlet.resource.ResourceHttpRequestHandler, which essentially streams out the content of the static files - You can like ResourceHttpRequestHandler, extend from org.springframework.web.servlet.support.WebContentGenerator which has extensive support for sending last-modified and caching related headers, and finally to stream the content also there is a utility that Spring provides:
org.springframework.util.FileCopyUtils.copy(resource.getInputStream(), response.getOutputStream());
Updated:
#Controller
public class HelloWorldController implements ApplicationContextAware {
ApplicatonContext ctx = ...;
#RequestMapping(value="/myProtectedContent")
public void index(HttpServletRequest req, HttpServletResponse res) {
Resource resource = ctx.getResource("classpath:staticpath/myjpg.jpg");
FileCopyUtils.copy(resource.getInputStream(), response.getOutputStream());
}
}
Something you can do is to map a new servlet to the path you want to be protected and handle the request the way you want.
For example, in web.xml:
<servlet>
<servlet-name>protServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/protServlet-context.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>protServlet</servlet-name>
<url-pattern>/myProtectedContent</url-pattern>
</servlet-mapping>
This way, you map a new servlet (DispatcherServlet) for URLs that are protected content.
The load-on-startup value equals 2 is due if you already have a DispatcherServlet with this field value equals 1.
Why does config.getInitParameter(String) always return null in the following code example?
public void init(ServletConfig config) throws ServletException
{
super.init(config);
filename = config.getInitParameter("addressfile");
This is web.xml file
<servlet>
<servlet-name>ListManagerServlet</servlet-name>
<servlet-class>savva.listmanagerservlet.ListManagerServlet</servlet-class>
<init-param>
<param-name>addressfile</param-name>
<param-value>d:\temp\demo.txt</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ListManagerServlet</servlet-name>
<url-pattern>/ListManagerServlet</url-pattern>
</servlet-mapping>
UPD: Eclipse EE Indigo, Java 1.6, Tomcat 7.0
The canonical way is to just use the inherited GenericServlet#getInitParameter() in the argumentless init() method (and remove any init(config) method).
#Override
public void init() throws ServletException {
filename = getInitParameter("addressfile");
}
If that still doesn't work, then your web.xml is not properly been deployed, or you have a typo in the parameter name, or you actually accessed a different instance variable than filename to use/test it.
Ensure your servlet is calling super.init(config) on its init method, else it won't work.
Make sure you have really deployed the proper web.xml. Also check with config.getInitParameterNames() what parameters have been found.
It's never a good idea to override the init(config) method. Instead use the provided init() convenience method and do a getServletConfig() to get the configuration parameters:
http://docs.oracle.com/javaee/1.2.1/api/javax/servlet/GenericServlet.html#init()
http://docs.oracle.com/javaee/1.2.1/api/javax/servlet/GenericServlet.html#getServletConfig()
If use the IDE STS4, checkout if annotation on the class name exists, use BOTH "annotation" and "web.xml" may cause the value null.
Why does config.getInitParameter(String) always return null in the following code example?
public void init(ServletConfig config) throws ServletException
{
super.init(config);
filename = config.getInitParameter("addressfile");
This is web.xml file
<servlet>
<servlet-name>ListManagerServlet</servlet-name>
<servlet-class>savva.listmanagerservlet.ListManagerServlet</servlet-class>
<init-param>
<param-name>addressfile</param-name>
<param-value>d:\temp\demo.txt</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ListManagerServlet</servlet-name>
<url-pattern>/ListManagerServlet</url-pattern>
</servlet-mapping>
UPD: Eclipse EE Indigo, Java 1.6, Tomcat 7.0
The canonical way is to just use the inherited GenericServlet#getInitParameter() in the argumentless init() method (and remove any init(config) method).
#Override
public void init() throws ServletException {
filename = getInitParameter("addressfile");
}
If that still doesn't work, then your web.xml is not properly been deployed, or you have a typo in the parameter name, or you actually accessed a different instance variable than filename to use/test it.
Ensure your servlet is calling super.init(config) on its init method, else it won't work.
Make sure you have really deployed the proper web.xml. Also check with config.getInitParameterNames() what parameters have been found.
It's never a good idea to override the init(config) method. Instead use the provided init() convenience method and do a getServletConfig() to get the configuration parameters:
http://docs.oracle.com/javaee/1.2.1/api/javax/servlet/GenericServlet.html#init()
http://docs.oracle.com/javaee/1.2.1/api/javax/servlet/GenericServlet.html#getServletConfig()
If use the IDE STS4, checkout if annotation on the class name exists, use BOTH "annotation" and "web.xml" may cause the value null.
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.