Does servlet not have any extensions? - java

I like to know like whenever user requests a jsp page we write hello.jsp or any html file we write hello.html or any image hello.jpeg.
My question is does servlet not have any extension ? Is it called directly called by name?

For Servlets, you have to explicitly tell the Servlet Container what URLs (either specific URLs or wildcards) map to what servlet. For example:
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>com.example.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
The above example would map the URL /hello to the servlet com.example.HelloWorld.
You can also do some wildcard mapping. For example:
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
This would map requests ending in ".html" to the HelloWorld servlet. But you aren't limited to any particular extensions. You could use anything you want:
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>*.foo</url-pattern>
</servlet-mapping>
All of this configuration takes place in your web application's web.xml file.

No, you have it fully in your own hands. It's not necessarily called by its name, it's called by its url-pattern. You can name it to whatever you want, e.g. /pages/* will run the servlet whenever you call http://example.com/pages/foo.jsp or http://example.com/pages/foo (which in turn gives the pathinfo back by request.getPathInfo(), so that you can determine what action to take and/or where to forward the request to). Or *.page which runs the servlet whenever you call http://example.com/foo.page (which in turn gives URI back by request.getRequestURI()).
To preprocess requests (when one requests a page for view) you normally use doGet() method. To postprocess requests (after a POST form submit), you normally use doPost() method.
You can in fact create as many servlets as you want, e.g. RegisterServlet listening on /register which is backed by a register.jsp as view and a LoginServlet listening on /login and backed by a login.jsp as view, etcetera. You can hide JSPs from direct access by placing them in /WEB-INF so that users are forced to call them through the servlet.
In MVC world, there's usually means of only one servlet listening on a certain url-pattern, which is called the Front Controller. In Sun JSF for example, there's the FacesServlet which runs whenever an URL matching by default *.jsf or /faces/* is called. In Apache Struts for example, there's the ActionServlet which listens on by default *.do. They determines which action to take and/or which view (the JSP file) to display based on the URL, request parameters and/or mappings. You're however free to change those default url-patterns. You can even change the default url-pattern of the JspServlet in servlercontainer's web.xml, which by default listens on *.jsp. It's however recommended to stick to a sensible and standardizedurl-pattern.
It might be interesting to know that any other "undefinied" URL patterns are covered by a "default" servlet. Check the servletcontainer's web.xml, you'll see one servlet which listens on / and thus in fact serves everything. It also manages display of directoy listings. In Tomcat for example it's called the DefaultServlet and described here.

Related

Java servlet regex url

I have a servlet. But instead of making that servlet listen to a static URL (e.g: /testservlet), I want it to listen to a dynamic url, or a URL with regex (e.g: /testservlet[0-9]*) which in this case will listen to a url mysite.com/testservlet followed by any numbers.
How do I do that?
Assuming that your servlet class is: victor.serlets.MyServlet
In your application's web.xml, include the following:
<servlet>
<servlet-name>myTestServlet </servlet-name>
<servlet-class>victor.serlets.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myTestServlet</servlet-name>
<url-pattern>/testservlet*</url-pattern>
</servlet-mapping>
As for the wildcard in the url-pattern tag, if you want to be more specific than "*" (i.e. anything), I am not certain what wildcards are supported. I suggest you look at the Oracle servlet docs for more info. FWIW, I think the "*" wildcard will suffice for your app.

how to detect popup call in java?

i am making the call to my servlet from javascript with window.open
window.open('myServlet','window','window Params')
i want to detect in myservlet whether call is coming from popup or not without passing any paramater alongwith servlet. Can we detect it in servlet?
You can with a query parameter
window.open('myServlet?from=popup','window','window Params');
Use that from parameter in your servlet to detect.
Edit:
Since you Don't want to use a query param, It's not possible on server side to know from where(click) this is getting called .
A query parameter would be best (as described by Suresh Atta). One possible alternative - since you can't use a parameter - might be to have two servlet mappings for your servlet - with one of the mappings for just the popup.
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>foo.bar.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/myServlet</url-pattern>
</servlet-mapping>
<!-- Popup-specific mapping -->
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/myServlet-popup</url-pattern>
</servlet-mapping>
Your popup could reference the popup-specific mapping:
window.open('myServlet-popup','window','window Params');
Then inside your servlet you could inspect the HttpServletRequest to determine the path that was used - and thus whether the servlet was called via the popup or not.
As already said, the parameter method is better. But this might be a possible alternative.

How to prepare view jsp using Liferay MVC Portlet

I have to write a portlet for Liferay Portal. Liferay provides convenient class MVCPortlet which allows to use simple routing for action phase using "name" attribute of <portlet:actionURL> tag and simple routing for view phase using mvcPath parameter for <renderURL> tag. What I miss, though, is the ability to prepare view for render phase. For example load some entities from database for referencing from the JSP.
I understand that I can include Java code in the JSP, but that's considered bad practice.
I use Liferay 6.1.
There's more than 1 ways to do what you want.
Make sure that a specific action is called before rendering a specific page. Fetch your resources there, and pass them as render parameters, so the jsp can get them. practically, you'll use actions to redirect to different pages
Override your render function, check the page that will be loaded, and fetch your resources if needed for the rendered jsp
use the resource phase in your jsp to call for the required resources.
Also make sure to understand the 2-phase (3-phase actually if you count the resource phase too) architecture of the MVCPortlet.
Now, about keeping the jsp code clean.. I'm not sure that the MVCPortet helps you do that. I've never seen an mvcPortlet project that doesn't have injected java snippets inside the jsp code here and there. For example, you'll have to use java code to read the request attributes
You need to define your different views (jsp) in portlet.xml's init-param tag as following:
<init-param>
<name>view-jsp</name>
<value>/jsp/view.jsp</value>
</init-param>
<init-param>
<name>edit-jsp</name>
<value>/jsp/edit.jsp</value>
</init-param>
<init-param>
<name>search-jsp</name>
<value>/jsp/search.jsp</value>
</init-param>
Send extra parameter from your portlet:renderURL / portlet:actionURL to indentify your action-type as
<portlet:actionURL var="editPageURL">
<portlet:param name="action" value="edit"/>
</portlet:actionURL>
Then in your (Java file), set global variable that is accessible in both phases (render / action) and filter action through type parameter as:
String action = ParamUtil.getString(request, "action", "view");
if(action.equals("edit")){
// do your work here
}else if(action.equals("search")){
}else{
}

Servlet mapping: url-pattern for URLs with trailing slash

I have a problem related to the servlet mapping. I have the following in web.xml:
<servlet>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>test.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
If I access to http://localhost:<port>/MyApp/HelloWorld the servlet HelloWorldServlet is called.
I also want my servelet to respond to http://localhost:<port>/MyApp/HelloWorld/. How can I achieve this effect? I'm developing with NetBeans but it does not allow me to put a pattern ended with /.
After you've added your wildcard on your <url-pattern>
<url-pattern>/HelloWorld/*</url-pattern>
You can get the extra path associated with the URL by using HttpServletRequest.getPathInfo().
E.g.
http://localhost:<port>/MyApp/HelloWorld/one/
The result will be
/one/
From the JavaDoc:
Returns any extra path information
associated with the URL the client
sent when it made this request. The
extra path information follows the
servlet path but precedes the query
string and will start with a "/"
character.
Use a wildcard. You can redirect all the traffic going to a specific URL to the same servlet. For example, you can add the following:
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/HelloWorld/*</url-pattern>
</servlet-mapping>
This will redirect the URL with a slash to your original servlet.
One thought - this would redirect anything to this URL pattern to the servlet. If you want to have other URL's past this URL, you should create a servlet that will redirect to the correct URL (by looking at the URL specified). Alternatively, you could use a framework that provides mapping for you.

Call a servlet on click of hyperlink

Is there a way to call a Java Servlet on click of hyperlink without using JavaScript?
Make the hyperlink have a URL that you have a servlet mapping defined for in the web.xml file.
The servlet-mapping element defines a mapping between a servlet and a URL pattern. The example below maps the servlet named myservlet to any URL that starts with /foo:
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>com.stackoverflow.examples.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/foo/*</url-pattern>
</servlet-mapping>
For this example, a hyperlink such as Click Me would invoke the servlet.
you declare your servlet in web.xml by setting its name, class and url-pattern
(let's say your url-pattern is /myServlet)
write mylink
override the doGet(..) method of the servlet to do whatever you want
Think that you've defined a servlet "callme" and web.xml has been configured for this servlet. Use the following syntax to call it using hyperlink
web.xml
<servlet>
<description>callme Functions</description>
<display-name>callme</display-name>
<servlet-name>callme</servlet-name> <servlet-class>com.test.Projects.callme</servlet-
class>
</servlet>
<servlet-mapping>
<servlet-name>callme</servlet-name>
<url-pattern>/callme</url-pattern>
</servlet-mapping>
in JSP:
Call the servlet
What exactly do you mean with "call a Java Servlet? The most normal (i.e. without any JavaScript magic) browser behaviour for clicking on a link is to send a HTTP request to fetch the document at the URL specified in the link and display it - and Servlets exist to respond to HTTP requests.
So you don't have to do anything special at all. Just have a regular HTML link and make sure that the servlet you want to "call" corresponds to that link's URL. Of course the next question is what that Servlet returns and what you want the browser to do with it.

Categories