How do I get the name of the currently executing script?

Use request.getRequestURI() or req.getServletPath(). The former returns the path to the script including any extra path information following the name of the servlet; the latter strips the extra path info.

 

The following example demonstrates it:

 

 URL  http://www.host.com/servlets/HelloEcho/extra/info?height=100&width=200

 

 getRequestURI   /servlets/HelloEcho/extra/info

 getServletPath  /servlets/HelloEcho

 getPathInfo     /extra/info

 getQueryString  height=100&width=200

 

 

This is useful if your form is self-referential; that is, it generates a form which calls itself again.

For example:

 

 out.println(“<FORM METHOD=POST ACTION=\”" + request.encodeURL(req.getServletPath()) + “\”>”);

 out.println(“<INPUT NAME=value>”);

 out.println(“<INPUT TYPE=submit>”);

 out.println(“</FORM>”);

 

Note: encodeURL() adds session information if necessary.

 

What is inter-servlet communication?

As the name says it, it is communication between servlets. Servlets talking to each other.

There are many ways to communicate between servlets, including :

 

    * Request Dispatching

    * HTTP Redirect

    * Servlet Chaining

    * HTTP request (using sockets or the URLConnection class)

    * Shared session, request, or application objects (beans)

What is a WAR file and how do I create one?

WAR (or “web archive”) file is simply a packaged webapp directory. It is created using the standard Java jar tool.

 

For example: jar cf ../mywebapp.war *

 

How can I call a servlet from a JSP page? How can I pass variables from the JSP that the servlet can access?

You can use <jsp:forward page=”/relativepath/YourServlet” /> or response.sendRedirect(“http://path/YourServlet”).

 

What is a web application (or “webapp”)?

A web application is a collection of servlets, html pages, classes, and other resources that can be bundled and run on multiple containers from multiple vendors. A web application is rooted at a specific path within a web server.

 

The contents of the WEB-INF directory are: /WEB-INF/web.xml deployment descriptor /WEB-INF/classes/* directory for servlet and utility classes. The classes in this directory are used by the application class loader to load classes from. /WEB-INF/lib/*.jar area for Java ARchive files which contain servlets, beans, and other utility classes useful to the web application. All such archive files are used by the web application class loader to load classes from.

What is a servlet bean?

A servlet bean is a serializable servlet that follows the JavaBeans component architecture, basically offering getter/setter methods.

How do I deal with multi-valued parameters in a servlet?

Instead of using getParameter() with the ServletRequest, as you would with single-valued parameters, use the getParameterValues() method. This returns a String array (or null) containing all the values of the parameter requested.

What is the difference between GenericServlet and HttpServlet?

GenericServlet is for servlets that might not use HTTP, like for instance FTP servlets. Of course, it turns out that there’s no such thing as FTP servlets, but they were trying to plan for future growth when they designed the spec. Maybe some day there will be another subclass, but for now always use HttpServlet.

 

In short GenericServlet is protocol independent, whereas HttpServlet is protocol dependent Generic servlet is used for small data transfer whereas HttpServlet is used for huge data transfer.

What is a servlet engine?

A “servlet engine” is a program that plugs in to a web server and runs servlets. The term is obsolete; the preferred term now is “servlet container” since that applies both to plug-in engines and to stand-alone web servers that support the Servlet API.

How can I download files from a URL using HTTP?

One way to do this is by using a URLConnection to open a stream to your desired URL, then copy the data out of the stream to a file on your local file system.

Can I invoke a JSP error page from a servlet?

Yes, you can invoke the JSP error page and pass the exception object to it from within a servlet. The trick is to create a request dispatcher for the JSP error page, and pass the exception object as a javax.servlet.jsp.jspException request attribute.

 

How can I download a file (for instance, a Microsoft Word document) from a server using a servlet and an applet?

Try telling the applet to call getAppletContext().showDocument()

 

In case or servlet, provide an anchor tag pointing to file that is to be downloaded.

 

out.println(“<a href=document1.doc>Document</a>”);

 

 

Can I call a JSP, then have it return control to the original JSP, like a subroutine or method call?

Yes. That is exactly the purpose served by the <jsp:include> action. The syntax of the include action is:

 

      <jsp:include page=”relativeURL” flush=”true” />

Follow

Get every new post delivered to your Inbox.