Q. How do you get your servlet to stop timing out on a really long database query? A. There are situations despite how much database tuning effort you put into a project, there might be complex queries or a batch process initiated via a Servlet, which might take several minutes to execute. The issue is that if you call a long query from a Servlet or JSP, the browser may time out before the call completes. When this happens, the user will not see the results of their request. There are proprietary solutions to this problem like asynchronous servlets in WebLogic, Async Beans in WebSphere etc but you need a solution that is portable. Let us look at portable solutions to this issue. |
Solution 1: Client-pull or client-refresh (aka server polling): You can use the tag for polling the server. This tag tells the client it must refresh the page after a number of seconds.
<META http-equiv=”Refresh” content=”10; url=”newPage.html” />
Once you can have the browser poll your Servlet on a regular basis to re-fetch a page, then your servlet can check for a value of a variable say in a HttpSession to determine if the page returned will have the results expected by the user or resend the tag with a “Please wait …” message and retry fetching the page again later.
Solution 2: JEE Solution: Instead of spawning your own threads within your Servlet, you could use JMS (Java Messaging Service). This involves following steps:
- You need to have two servlets, a RequestingServlet and a DisplayingServlet. The initial client request is sent to the RequestingServlet. Both the RequestingServlet and DisplayingServlet polled by the browser via tag discussed above or JavaScript. Both these Servlets should send the tag with their responses until final display of the query results.
- RequestingServlet places the query on the “request” queue using JMS.
- You need to have a MessageDrivenBean (aka MDB) say QueryProcessorMDB, which dequeues the query from the “request” queue and performs the long-running database operation. On completion of processing long-running database operation, the QueryProcessorMDB returns the query results to the “reply” queue (use javax.jms.QueueSender & javax.jms.ObjectMessage). Note: MDBs are invoked asynchronously on arrival of messages in the queue.
- DisplayingServlet checks the “reply” queue for the query results using JMS (use javax.jms.QueueReceiver & javax.jms.ObjectMessage) every few seconds via tag described above or a JavaScript.
Advantages: Firstly, implementing your long-running database operation to be invoked from onMessage( ) method of your QueryProcessorMDB decouples your application whereby if a database failure occurs, the request query message will be placed back in the “request” queue and retried again later. Secondly MDBs can be clustered (with or without additional JVMs) to listen on the same “request” queue. This means cluster of MDBs will be balancing the load of processing long running database operations. This can improve the throughput due to increased processing power.
Q. What is the difference between forwarding a request and redirecting a request?
A. Both methods send you to a new resource like Servlet, JSP, etc
Forward:
- Forward action takes place within the server without the knowledge of the browser. Accepts relative path to the servlet or context root.
- No extra network trip.
Redirect:
- Sends a header back to the browser, which contains the name of the resource to be redirected to. The browser will make a fresh request from this header information. Need to provide absolute URL path.
- This has an overhead of extra remote trip but has the advantage of being able to refer to any resource on the same or different domain and also allows book marking of the page.
A.
|
Q. How do you perform I/O operations in a Servlet/JSP?
A.
Problem: Since web applications are deployed as WAR files on the application server’s web container, the full path and relative paths to these files vary for each server.
Solution -1: You can configure the file paths in web.xml using
Solution -2: You can overcome these configuration issues by using the features of java.lang.ClassLoader and javax.servlet.ServletContext classes. There are various ways of reading a file using the ServletContext API methods such as getResource(String resource),getResourceAsStream(String resource), getResourcePaths(String path) and getRealPath(String path). The getRealPath(String path) method translates virtual URL into real path.
//Get the file “products.xml” under the WEB-INF folder of your application as inputstream
InputStream is = config.getServletContext().getResourceAsStream(“/products.xml”);
Alternatively you can use the APIs from ClassLoader as follows. The file “products.xml” should be placed under WEB-INF/classes directory where all web application classes reside.
//Get the URL for the file and create a stream explicitly
URL url = config.getServletContext().getResource(“/products.xml”);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream));
OR
//use the context class loader
URL url = Thread.currentThread().getContextClassLoader().getResource(“products-out.xml”);
BufferedWriter bw = new BufferedWriter(new FileWriter(url.getFile());
Q. How do you send a file to a browser from your web application? I.e. how do you download a file from your web application?
A. Files can be downloaded from a web application by using the right combination of headers.
//set the header to a non-standard value for attachments to be saved by the browser with the //Save-As dialog so that it is unrecognized by the browsers because often browsers try to do //something special when they recognize the content-type.
response.setContentType(“application/x-download”);
//use Content-Disposition “attachment” to invoke “Save As” dialog and “inline” for displaying //the file content on the browser without invoking the “Save As” dialog.
response.setHeader(“Content-disposition”, “attachment;filename=” + fileName);
Q. How do you send a file from a browser to your web application? i.e. How do you upload a file to your web application?
A. There are better and more secured ways to upload your files instead of using using web. For example FTP, secure FTP etc. But if you need to do it via your web application then your default encoding and GET methods are not suitable for file upload and a form containing file input fields must specify the encoding type “multipart/form-data” and the POST method in the
0 komentar:
Posting Komentar