Place your text ad here.
World class Hard Drive Recovery and renowned raid recovery services
WestNIC provides reliable web hosting services
Free software downloads and drivers download resources
This FAQ is part of the Code Style Help and FAQ section. Use the help request form below if your question is not answered here, but make sure you are asking the right question first.
doPost() method from a hyperlink?
static variable or Hashtable to store a list of strings?
A: Yes, you can catch servlet errors and give custom error pages for them, but if there are exceptional conditions you can anticipate, it would be better for your application to address these directly and try to avoid them in the first place.
More details available to subscribers:
Can I catch an exception and give my own error message?
A: To achieve an include scheme in a servlet, use javax.servlet.RequestDispatcher, which takes the path of the target document in its constructor. A RequestDispatcher can be obtained from the ServletContext object (via ServletConfig) in your servlet's init method, or from ServletRequest or HttpServletRequest in doGet or doPost.
doPost() method from a hyperlink?
A: Following a hyperlink in a Web document normally creates an HTTP GET request to the given URL, not a POST request. Post requests are normally produced by the submission of an HTML form whose method attribute is post. In this case any values given in the form's input elements are passed as request parameters to the URL specified in the form's action attribute.
More details available to subscribers:
Can I call the doPost() method from a hyperlink?
A: You can obtain the operating system, operating system version and Java version for a servlet container through standard Java system properties. There is a wide range of standard properties that can be obtained from the static System class method getProperty(String), using the following keys:
String osName = System.getProperty("os.name");
String osVersion = System.getProperty("os.version");
String javaVersion = System.getProperty("java.version");
These system properties are common to all Java runtime systems including servlets.
static variable or Hashtable to store a list of strings?
A: The static modifier for a variable is different from the object type of the variable. If you want all servlet instances to be able to read and write data to a field, it should be static. Depending on the nature of the application, it may also be necessary to synchronize access to the variable.
If you want to store a simple list of strings, a java.util.List type variable would be most appropriate. A Vector is a List type and its implementation is synchronized.
private static final List stringList = new Vector();
A: A file upload servlet should control what type of files are permitted, to process the byte stream and decide where the content is stored. As you can imagine, allowing anybody to upload any file to your Web server is a significant security hazard, so you must be very careful to ensure that you restrict who has access, what they upload, and that the public do not have arbitrary access to the file via the Internet.
The Apache commons file upload package would be a good place to start.
A: This example code is for the Apache commons file upload package.
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Set factory constraints
factory.setSizeThreshold(yourMaxMemorySize);
factory.setRepository(yourTempDirectory);
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Set overall request size constraint
upload.setSizeMax(yourMaxRequestSize);
A: The ServletRequest type has a getRemoteAddr() method that returns the Internet Protocol (IP) address of the client that made the request. The address is returned as a String, so must be parsed further if you want to extract numeric references from it. In JSP you can access this method it directly through the request variable, as below.
More details available to subscribers:
Can I get the client IP and MAC address?
A: If your servlet container is listening on port 8080 then the HttpServletRequest getServerPort() method will return the int 8080.
A: There are two main approaches to checking request parameters: either look up named parameters you expect to be present, or iterate through all parameters. It is always possible there is more than one value for each named parameter, so look up values using the getParameterValues(String) method, which takes the name of the parameter as an argument and returns a string array. You need to decide what should happen if there is more than one value, this example takes the first value in the array.
More details available to subscribers:
How can I check what parameters are available in the request?
A: The simple form below invites people to input their name.
<form action="http://www.codestyle.org/servlets/EchoRequest"> <input type="text" name="name" /> <input type="submit" /> </form>
To handle this submission, extend HttpServlet and provide a doPost(HttpServletRequest, HttpServletResponse) method, as below.
More details available to subscribers:
How do I get parameter values from a form submission?
A: To pass non-ASCII characters in servlet parameters without them being interpreted as part of the URL structure, you must encode the characters as hexadecimal (base 16) values prefixed by the percent symbol, %. This is known as URL-encoding and is normally done automatically by Web browsers when you submit a form. For instance, ampersand is ASCII character number 38, which is %26 expressed as a hexadecimal. See the table below for other conversion values.
More details available to subscribers:
The & in the parameter value truncates the string!
A: In design terms, it is not a good idea for your application to have different outcomes from equivalent actions unless the consequences are quite clear to the person using the form. This is especially important for people with impaired vision, or those using screen readers.
More details available to subscribers:
How can I tell which submit button was activated?
A: You should not rely upon the HTTP referer (sic) header to be sent to your servlet because some Web browsers (and other client types) do not send them for privacy reasons.
More details available to subscribers:
Which link was followed to request my servlet?
A: The binary file format of Microsoft Excel documents is extremely complex. If you want to reproduce this format in detail and with precision you should read OpenOffice.org's Documentation of the Microsoft Excel File Format.
More details available to subscribers:
How can I make the servlet output open in Microsoft Excel?
A: To display HTML output in plain text format, you must set the HTTP Content-Type header to text/plain, as below:
More details available to subscribers:
How can I show my HTML markup as plain text?
A: It can be very tricky inserting inline Javascript in servlet output because of the level of quoted output escaping that can be necessary. If you miss or fail to correctly escape a literal quote, your Javascript will not parse correctly. For simplicity, it would be preferable to use an external Javascript file.
More details available to subscribers:
How can I fix the Javascript in my servlet?
A: It is best to use an external stylesheet reference, so that you can edit your servlet and CSS rules independently.
More details available to subscribers:
How do I include CSS in my servlet?
A: create an instance of your SpellChecker class in the doGet() or doPost() method of your servlet and pass the relevant parameter to it as a string, as in the code sample below ...
More details available to subscribers:
How can I pass user input through my spelling checker?
A: The applet sandbox security environment only permits connections with the host from which the applet was loaded. If you attempt a connection with a different host, the applet will throw a SecurityException and fail. This can also cause problems when you are testing an applet loaded from the local file system, which has no host.
More details available to subscribers:
How do I send data from an applet to a servlet?
| Front-end FAQs | Back-end FAQs | Learning Java |
|---|---|---|
See site help for questions about this site, our text ads and sponsored links services.