Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 93 94 95 96 97 98 < 99 > 100 101 102 103 104 105 .. 166 >> Next

Pattern pattern = Pattern.compile("<(a|A) href+[*<]*</(a|A)>"); Matcher match = pattern.matcher(pageOutput);
%>
<tr bgcolor="#eeeeee">
<td align="center"> Links for next Level: <%= filename %></td>
</tr>
<%
// display all the references found in the URI while (match.find()) {
// split words
String[] sw = match.group(0).split("[\"]");
if ((sw.length > 1) && (sw[1].startsWith("http://")) && ^
(sw[1].endsWith("html"))) {
%>
<tr><td>
<%
out.println("webpage=" + sw[1]); vRef.addElement(sw[1]);
%>
</td></tr>
<%
}
}
The Web page displayed in Figure 30.2 is the result of the spidering action of the code snippet above, which resides in the regexpTest.jsp application. The results shown below the Submit button are the links parsed from the Web page requested in the URI field.
Java Regular Expressions are a powerful new language construct that strengthens string manipulation activities for developers. With Java Regular Expressions, Java developers can realize in their text processing activities what Perl programmers have been lauding about for years.
Instance Variables in Servlets 269
3 Spider - Microsoft Internet Explorer provided by @Home
I File Edit View Favorites Tools Help CJ
+*Back Search _?jFavoites ^History | ^
Address |?] http7/tocalhotf:8080/regexSpidef/fegexpTe$tj$p *| ^Go Links *
Go glc^l 1 fcfr Seech Web ^Search Site Page Info fr~jUp w ^HigWgl
Spider -

UM: |httpy/localhost8080/regexSpiderAest.html
Levels to -^-i spider: 1
| Submit | 1
Links for next Level: test.html
[webp age=http /lo calhost8080/regexSpider/index 1. html
webp age=http /lo calhost8080/regexSpider/index2. html
Links for next Level: index 1 html
Links for next Level: index2.html
webp age=http /lo calhost8080/regexSpider/index2 2. html
J
Done ^ Local intranet
a Start I $ a jjc | | fflc | MC.IIiiir | V | ) . | | )0811] 9:52 AM
Figure 30.2 Spider results.
Item 31: Instance Variables in Servlets
A common trap that new servlet developers find themselves in revolves around the use of instance variables. Unfortunately, the symptoms of this problem are not easy to diagnose until the last minute. The developer writes the servlet, and it goes through standalone testing just fine. When it is load-tested (or when it goes into production with many concurrent users), however, strange things start to occur on an ad hoc basis: garbled strings of nonsense begin appearing in Web browsers, users of the enterprise Web system begin receiving other users' information, and seemingly "random" errors appear in the application. What went wrong?
A simple example of this situation can be seen in Listing 31.1, where we have an example application that serves as a library of technical resources. Our fictional "Online Technobabble Library" is a document repository where multiple users can check out, check in, and read multiple documents with a lot of technobabble. The servlet takes a parameter, userid, which tells the servlet where to get the user's information.
270 Item 31
001 package org.javapitfalls.item31;
002 import java.io.*;
003 import java.text.*;
004 import java.util.*;
005 import javax.servlet.*;
006 import javax.servlet.http.*;
007 /**
008 * This example demonstrates using instance variables
009 * in a servlet.. The example features a fictional
010 * "TechnoBabble Library", where users can check out
011 * and check in technical documentation.
012 */
013 public class BadTechnobabbleLibraryServlet
014 extends HttpServlet
015 {
016
017 PrintWriter m_out = null;
018 String m_useridparam = null;
019
020
021 /**
022 * doGet() method for a HTTP GET
023 *
024 * @param request the HttpServletRequest object
025 * @param response the HttpServletResponse object
026 */
027 public void doGet(HttpServletRequest request,
028 HttpServletResponse response)
029 throws ServletException, IOException
030 {
031 String title = "Online Technobabble Library";
032 response.setContentType("text/html");
033 m_out = response.getWriter();
034 m_useridparam = request.getParameter("userid");
035
036 m_out.println("<HTML>");
037 m_out.println("<TITLE>" + title + "</TITLE>");
038 m_out.println("<BODY BGCOLOR='WHITE'>");
039 m_out.println("<CENTER><H1>" + title +
040 "</H1></CENTER>");
041 m_out.println("<HR>");
042
043 //This will put the user's personal page in..
044 putInUserData();
045 m_out.println("<HR>");
046 m_out.println("</BODY></HTML>");
047 }
048
049
Listing 31.1 A bad example!
Instance Variables in Servlets 271
050 /**
051 * doPost() method for a HTTP PUT
052 *
053 * @param request the HttpServletRequest Object
054 * @param response the HttpServletResponse Object
055 */
056 public void doPost(HttpServletRequest request,
057 HttpServletResponse response)
058 throws ServletException, IOException
059 {
060 doGet(request, response);
061 }
062
063
064 /**
065 * This method reads the user's data from the filesystem
Previous << 1 .. 93 94 95 96 97 98 < 99 > 100 101 102 103 104 105 .. 166 >> Next