Tomcat connectivity restrictions
Abstract: tomcat connectivity restrictions
</ Td> </ tr> <tr> <td width="417" height="35" valign="top" class="ArticleTeitle">
In the past few days the number of on-line systems are more than ever, with a few increases, a servlet can not return to normal data errors, users can create normal landing, and found dead on the server what abnormal. That the initial allocation of individual computers, although sometimes can not explain the phenomenon sometimes not, did not make serious consideration. Today the customer more and more must be done to seriously consider, although very busy, but also have to confront this issue to get it. Consider a system of written procedures for worse, that is certainly what improper handling procedures, and the crazy part of the relevant inspection procedures, which is not any other problem. I do not know what we should do at the time, whether the problem is suddenly the connection configuration tomcat from smaller?
Server.xml was opened, linking the allocation of places to find the original part of the connector maxProcessors only 75, really is a small, modify preserve and reopen. ↑ Back Abstract: Tomcat virtual directory settings </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> tomcat installed, you only need to put the package $ Tomcat_Home $ / webapps under the can be directly used. This will give webapps increasingly on the need for virtual directory: <table Width="674" border="0"> <tr> <td width="400"> 2. Before we know through the $ tomcat_home $ / conf / catalina / localhost directory will generate the corresponding configuration file, Therefore, we can directly in the $ tomcat_home $ / conf / catalina / localhost directory write arbitrary configuration file from the file name, preferably with your web application to manage the same, such as the site.xml content Ibid. ↑ Back Abstract: Based on the simple UDP Client / Server Programming 1. Customers, procedures QuoteClient.java Abstract: shielding tomcat directory list functionality </ Td> </ tr> <tr> <td width="506" height="35" valign="top" class="ArticleTeitle"> Debug 0 Listings True Debug 0 Listings False ↑ Back Abstract: jsp read attribute server </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> ↑ Back Abstract: Tomcat 5.5.11 get started </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="735" border="0"> <tr> <td> </ td> </ tr> </ table>
Jakarta Tomcat project is an important component, is not recommended for sun and jsp servlet containers, jsp servlet and the latest standards can be found in the Tomcat, it is best for beginners get started jsp tools. First, the installation Web.xml file reads as follows: <? Xml version = "1.0" encoding = "ISO-8859-1"> Then html, jsp files can be put under the test directory for testing such as the establishment of a test.jsp as follows: Function TempSave (ElementID) (CommentsPersistDiv.setAttribute ( "CommentContent" document.getElementById (ElementID). Value); CommentsPersistDiv.save ( "CommentXMLStore");) function Restore (ElementID) (CommentsPersistDiv.load ( "CommentXMLStore"); document . getElementById (ElementID). CommentsPersistDiv.getAttribute value = ( "CommentContent");) </ td> </ tr> <tr>
↑ Back Abstract: 2_0 achieve Struts form data validation Http://localhost:8080/Struts2_Validation/HelloWorld.action-type, enter Locale zh_CN and click "Submit" to the results shown in the above articles. In the server console the following output: The above described Struts 2.0 output data validation methods of work, it requires the following steps: The text does not like to see friends, can refer to Figure 1 below. You will see here may doubt: "So many places can check form data, in the end I should do there?" Choice is a good thing, but the decision process is often painful, people are often at a loss. If you reference the following suggestions, I believe it will be easier to make the right choice. Running the above example, in the Locale enter zh and submitted a page shown in Figure 2. Enter de_DE in Locale, a page shown in Figure 3. On the content of a program on how to achieve verification, which are part of the monotonous repetition. More often, we use Struts 2.0 validation framework configured to achieve some common validation. I have a habit of learning programming - like to look at the output, look at code. This study is look at the benefits of learning the results can stimulate the passion, we could also look at the code before thinking about how to achieve their own, and then look at the problem with the code, it more clear. We will therefore make the following presentation. First, under the new ValidationAction.java tutorial package, the code below: Then, out of the above configuration Ation, the following code fragment: Then, create and Output.jsp Input.jsp contents are as follows: Then, in the tutorial package ValidationAction created under the configuration file check-validation.xml Xxx (Xxx Action for the class), in this case the file name ValidationAction-validation.xml, as follows: Published running applications, http://localhost:8080/Struts2_Validation/Input.jsp type in the address bar, as shown in Figure 4 pages. Click on "Submit" to the form, as shown in Figure 5 pages. Required String casually in the filling point things go Output.jsp page, as shown in Figure 6. Through the example above, we can see that the use of the validation framework is very simple. However, the two cases are inadequate: Of course, it is necessary to improve the above shortcomings, Struts 2.0, the only Xiaocaiyidie. Below are the specific realization, the first international document of resources into an error message, then, according to the realization of the above. Because of the use of Javascript on the client side show error messages, so loading Input.jsp pages, Struts 2.0 of the need for international string, and we need to use to access Action Input.jsp pages, please refer to concrete realization "In the international Struts 2.0 (i18n) your application" in the last part. Finally released running applications, directly in the page click on "Submit", the form has not been submitted and occurs, as shown in Figure 7: Check through the framework to achieve validation interceptors, the bar was registered to set the default interceptor chain. It conversionError interceptors, in validateXxx () is called before. Here there was a matter of choice: in the end should be in action through validateXxx () or validate () to achieve validation, validation or the use of interceptors? The vast majority of cases, I suggest that you check the use of the framework, only when the framework can not meet your request to prepare their own code. In the above example, we have created ValidationAction validation.xml to configure form-validation. Struts 2.0 validation framework will automatically read the document, but it will leads to a problem - if I Action Action succession of other categories, which are Class Action needs to form data validation, I whether in the category of the configuration file (Xxx-validation.xml) copy of the configuration father? The answer is no, because the validation framework Struts 2.0 with the "international Struts 2.0 (i18n) Your application" referred to the "resource documents View order," similar to a specific configuration files View order. The difference is that the validation framework in accordance with the order from top to bottom in the class hierarchy View configuration files. Assume that the establishment of the following conditions: If Dog to be check, framework approach will find the following configuration file (which is the alias in struts.xml Action in the definition of aliases): Struts 2.0 has been used a lot for you validation, following in the jar in the default.xml registered validators. Check framework can be used to facilitate the realization of form data validation, verification and Action can be separated, we should as far as possible the use of validation framework. In the use of validation framework, please do not forget that through the resource file by adding invalid.fieldvalue.xxx string that fit the type conversion error messages or use conversion validators. ↑ Back Abstract: The simple message of this confused Struts </ Td> </ tr> <tr> <td width="496" height="35" valign="top" class="ArticleTeitle"> ################ ################################## ↑ Back Abstract: more customer support client / server programming In front of the Client / Server process can only achieve Server and client dialogue. In practical applications, often in the server running on a permanent procedure, it can receive a number of other client's request, provide the necessary services. In order to achieve the server side, providing services to more than one function, the need for transformation of the above procedures, the use of multi-threaded, multi-mechanism. Always the designated server port monitor whether there is a customer request, once listening to customer requests, the server will start a thread specialized services to respond to the customer's request, or the server itself in the End threads start immediately after and entered eavesdropping state, waiting for the next customer to arrive. Abstract: another tomcat web application under the build.xml file </ Td> </ tr> <tr> <td width="513" height="35" valign="top" class="ArticleTeitle"> <? Xml version = "1.0"> ↑ Back
EnableLookups = "true" redirectPort = "8443"
AcceptCount = "250" debug = "0" connectionTimeout = "20000"
UseURIValidationHack = "false" />
</ Td> <td width="267" align="center" valign="top" class="ArticleTeitle">
</ Td> </ tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle">
Tomcat virtual directory settings
1. Single application settings:
% Open Tomcat_Home% \ conf \ server.xml documents in the
Parameters: path that the browser input http://localhost:8080/site
DocBase that your package is the physical path, the default is $ absolute path Tomcat_Home $ / webapps
This will be set after the $ tomcat_home $ / conf / catalina / localhost directory site.xml generate a document, the contents of this document is to set up just the content.
3. Changes the entire root directory:
Find
AppBase set to be the desired path (absolute path)
Emerging issues: Admission: http://localhost:8080/ not the default page of the reasons is of course changes the path. Solution is very simple, the paper copy from, or then the original set as the default virtual path.
</ Td> <td width="264" align="right"> </ td> </ tr> </ table>
</ Td> </ tr> <tr>
Based on the simple UDP Client / Server Programming
Import java.io. *;
Import java.net .*;
Import java.util .*;
(Public class QuoteClient
Public static void main (String [] args) throws IOException
(
If (args.length! = 1) (
/ / If the start time did not give the names of Server, then withdraw from the mistakes System.out.println ( "Usage: java QuoteClient
/ / Print wrong information return; / /)
DatagramSocket socket = new DatagramSocklet ();
/ / Create Datagram Sockets
Byte [] = new byte buf [256]; / / create buffer InetAddress address = InetAddress.getByName (args [0]);
/ / From the command line parameters are given the first name of the default Server through its been Server IP information DatagramPacket packet = new DatagramPacket (buf, buf.length, address, 4445);
/ / Create DatagramPacket object socket.send (packet); / / send packet = new DatagramPacket (buf, buf.length);
/ / Create a new DatagramPacket object to receive data at socket.receive (packet); / / receiver String received = new String (packet.getData ());
/ / According to receive an array of bytes to generate the corresponding string System.out.println ( "Quote of the Moment:" + received);
/ / Print generated string
Socket.close () / / close socket)
)
2. Server side procedures: QuoteServer.java
(Public class QuoteServer
Public static void main (String args []) throws java.io.IOException
(
New QuoteServerThread (). Start ();
/ / Start a thread QuoteServerThread)
)
3. Procedures QuoteServerThread.java
Import java.io. *;
Import java.net .*;
Import java.util .*;
/ / Server thread public class QuoteServerThread extends Thread
(
Protected DatagramSocket socket = null;
/ / Record and the associated object protected BufferedReader in DatagramSocket target = null;
/ / Used to read a Reader document
Protected boolean moreQuotes = true;
/ / Variable signs, and whether to continue to operate
Public QuoteServerThread () throws IOException (
/ / No parameters of this structure function ( "QuoteServerThread");
/ / QuoteServerThread call with default values for parameters constructor)
Public QuoteServerThread (String name) throws IOException (
Super (name) / / father called the constructor function socket = new DatagramSocket (4445);
/ / Port in 4445 to create data reported socket try (
In = new BufferedReader (new FileReader ( "one-liners.txt"));
/ / Open a file, the corresponding structure BufferReader target) catch (FileNotFoundException e) (/ / Exception Handling System.err.println ( "Could not open quote file. Serving time instead.");
/ / Print error message)
)
Public void run () / / (main thread
While (moreQuotes) (
Try (
Byte [] = new byte buf [256]; / / create buffer DatagramPacket packet = new DatagramPacket (buf, buf.length);
/ / From the buffer zone structure DatagramPacket object socket.receive (packet); / / receive data reported String dString = null;
If (in = = null) dString = new Date (). ToString ();
/ / If the initialization of the failure to open the file,
/ / Used as a date to send the string else dString = getNextQuote ();
/ / Otherwise call from the member function in the document read out dString.getByte string buf = ();
/ / String into the byte array, in order to send
Packet.getAddress InetAddress address = ();
/ / Client-to be sent Packet Address Client packet.getPort int port = () / / and port packet = new DatagramPacket (buf, buf.length, address, port);
/ / Client information under the Construction DatagramPacket
Socket.send (packet); / / send data at) catch (IOException e) (/ / Exception Handling e.printStackTrace (); / / Print the wrong stack moreQuotes = false; / / false signs variable home to end the cycle)
)
Socket.close () / / close Datagram Sockets)
Protected String getNextQuotes () (
/ / Member function, in reading data from the document String returnValue = null;
Try (
If ((in.readLine returnValue = ()) = = null) (
/ / Read from his document, if the document tail in.close read () / / input stream closed moreQuotes = false;
/ / Variable home false signs to end the cycle returnValue = "No more quotes. Goodbye.";
/ / Return home value) / / string which is otherwise return from the document read out a string) catch (IOEception e) (/ / Exception Handling returnValue = "IOException occurred in server";
/ / Home abnormal return value)
Return returnValue; / / string)
)
We can see that the use of TCP and UDP use of the procedures there is still great distinction. A more obvious difference between them is that the UDP Socket Programming is not to provide monitoring functions, which means that the two sides more equal communications, in the face of the interface is exactly the same. However, in order to achieve using UDP C / S structure, the use of UDP can be used DatagramSocket.receive () to achieve similar to the monitoring function. Because receive () function is blocked, and when it returned, the buffer zone had already been filled received a reported data, and can be reported from the data sent to the various kinds of information, accept that with the () is very alike, and therefore can read data to decide on the next action, which reached a monitoring network with similar results.
↑ Back Shielding tomcat directory list functionality
Shielding tomcat directory list functionality
By default, if you visit tomcat of a web application, and if you type is a directory name, and the directory can be the welcome absence of a document, then the directory will tomcat all the documents out, if you want to block this default behavior, it can be amended conf / web.xml file, which will be:
Is amended as follows:
</ Td> <td width="178" align="center" valign="top" class="ArticleTeitle">
</ Td> </ tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle">
Jsp read attribute server
<table Width="734" border="0"> <tr> <td width="728"> </ td> </ tr> </ table>
<% @ Page contentType = "text / html; gb2312 charset ="%>
<% @ Page import = "java.util .*"%>
<head>
</ Head>
<body Bgcolor="#FFFFFF" text="#000000" leftmargin="10" topmargin="10" marginwidth="0" marginheight="0">
<table Width="80%" border="0" cellspacing="1" cellpadding="4">
<tr>
<td Colspan="2" bgcolor="#8CC2E3"> JSP Info v1.0 </ td>
</ Tr>
<tr>
<td Colspan="2" bgcolor="#DEEEF8"> Author: Snowman Web:
Http://www.xinboard.net
</ Td>
</ Tr>
<tr Bgcolor="#FFFFFF">
<td Colspan="2" height="50"> </ td>
</ Tr>
<tr Bgcolor="#B4D8ED">
<td Width="30%"> Environment (Environment) </ td>
<td Width="70%"> Values (value) </ td>
</ Tr>
<tr Bgcolor="#DEEEF8">
<td Bgcolor="#DEEEF8"> Server name (the name of the server) </ td>
<td> <% = Request.getServerName ()%> </ td>
</ Tr>
<tr Bgcolor="#DEEEF8">
<td> Server port (server ports) </ td>
<td> <% = Request.getServerPort ()%> </ td>
</ Tr>
<tr Bgcolor="#DEEEF8">
<td> Remote IP address (the client IP address) </ td>
<td> <% = Request.getRemoteAddr ()%> </ td>
</ Tr>
<tr Bgcolor="#DEEEF8">
<td Colspan="2"> Context attributes (associated attributes) </ td>
</ Tr>
<%
ServletContext context = getServletContext ();
Enumeration enum = context.getAttributeNames ();
While (enum.hasMoreElements ()) (
String key = (String) enum.nextElement ();
Object value = context.getAttribute (key);
Out.println ( "<tr bgcolor=#DEEEF8> <td>" + key + "</ td> <td>" + + value, "</ td> </ tr>");
)
%>
<tr Bgcolor="#FFFFFF" valign="bottom">
<td Colspan="2" height="50"> End (concluded) </ td>
</ Tr>
</ Table>
</ Body>
</ Td> </ tr> <tr>
Tomcat 5.5.11 get started
1, as an example to win2000: sun from the official Web site jdk1.5 (java2 platform Professional Edition 5.0) above, and installed in c: \ jdk1.5.
2, tomcat from the official website and download the latest version of jakarta-tomcat-5.5.11.exe following the prompts to install c: \ Tomcat 5.5.
3, copying c: \ jdk1.5 \ lib \ tools.jar to c: \ Tomcat 5.5 \ common \ lib under.
4, setting up the environment variables:
Java_Home = c: \ jdk1.5
Tomcat_Home = c: \ Tomcat 5.5
Second, click start: Start -> procedure -> Apache Tomcat 5.5-> Monitor Tomcat, and then double-click the icon on the task bar, windows are open, click start.
3, testing in the browser input http://127.0.0.1:8080/ test, as tomcat itself with the web server without the need to install web server.
4, html, jsp test our own web directory of the project should be focused on Tomcat 5.5 \ webapps below.
First established in a test webapps folder, in the test folder creation of a WEB-INF directory inside Add web.xml file, the directory structure is as follows:
[Test]
[WEB-INF]
Web.xml
Xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
Version = "2.4">
Welcome to Tomcat
<% @ Page language = "java"%>
<% @ Page contentType = "text / html; gb2312 charset ="%>
<% @ Page import = "java.util .*"%>
<% = New Date (). ToString ()%>
Http://localhost:8080/test/test.jsp can be called by the current date and time.
4. Servlet test configuration
1, to prepare and compile the following simple servlet, the compiler will be tomcat 5.5 \ common \ lib \ servlet in the category-api.jar Add document search path.
Import java.io. *;
Import javax.servlet .*;
Import javax.servlet.http .*;
Public class HelloWorld extends HttpServlet (
Public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException (
Response.setContentType ( "text / html");
PrintWriter out = response.getWriter ();
Out.println ("");
Out.println ( "Hello World");
)
)
2, in the test / WEB-INF directory to build a classes directory, the directory structure is as follows:
[Test]
[WEB-INF]
[Classes]
Web.xml
Compiler will be available to the success of HelloWorld.class classes directory, web.xml file to read as follows:
<? Xml version = "1.0" encoding = "ISO-8859-1">
Xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
Version = "2.4">
Welcome to Tomcat
Restart Tomcat 5.0
By calling http://127.0.0.1:8080/test/HelloWorld hello world can see the output (download the source code) Struts 2_0 achieve in the form data validation
Calling validateExecute () by reflection …
Calling validate ()…
Calling execute ()…
Converting Locale to String …
Figure 1 calibration sequence diagram
Figure 2 the wrong format
Figure 3 wrong values Check use Struts Framework 2.0
Import com.opensymphony.xwork2.ActionSupport;
Public class ValidationAction extends ActionSupport (
Private String reqiuredString;
Public String getReqiuredString () (
Return reqiuredString;
)
Public void setReqiuredString (String reqiuredString) (
This. ReqiuredString = reqiuredString;
)
@ Override
Public String execute () (
Return SUCCESS;
)
)
<Result> / Output.jsp result >
<Result name = "input"> / Input.jsp result >
action >
<% @ Taglib prefix = "s" uri = "/ struts-tags"%>
<Html>
<Head>
<Title> Hello World title >
<S: head />
head >
<Body>
<S: form action = "ValidationAction">
<S: textfield name = "reqiuredString" label = "Required String" />
<S: submit />
s:form >
body >
html > Input.jsp
<% @ Page contentType = "text / html; charset = UTF-8"%>
<% @ Taglib prefix = "s" uri = "/ struts-tags"%>
<Html>
<Head>
<Title> Hello World title >
head >
<Body>
Required String: <s: property value = "reqiuredString" />
body >
html > Output.jsp
"- / / OpenSymphony Group / / XWork Validator 1.0 / / EN"
"Http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<Validators>
<Field name = "reqiuredString">
<Field-validator type = "requiredstring">
<Message> This string is required message >
field-validator >
field >
validators >
Figure 4 Input.jsp
Figure 5 error messages
Figure 6 Output.jsp
Figure 7 Client Verification View configuration file order
Existing validator
<Validator name = "required" class = "com.opensymphony.xwork2.validator.validators.RequiredFieldValidator" />
<Validator name = "requiredstring" class = "com.opensymphony.xwork2.validator.validators.RequiredStringValidator" />
<Validator name = "int" class = "com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator" />
<Validator name = "double" class = "com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator" />
<Validator name = "date" class = "com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator" />
<Validator name = "expression" class = "com.opensymphony.xwork2.validator.validators.ExpressionValidator" />
<Validator name = "fieldexpression" class = "com.opensymphony.xwork2.validator.validators.FieldExpressionValidator" />
<Validator name = "email" class = "com.opensymphony.xwork2.validator.validators.EmailValidator" />
<Validator name = "url" class = "com.opensymphony.xwork2.validator.validators.URLValidator" />
<Validator name = "visitor" class = "com.opensymphony.xwork2.validator.validators.VisitorFieldValidator" />
<Validator name = "conversion" class = "com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator" />
<Validator name = "stringlength" class = "com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator" />
<Validator name = "regex" class = "com.opensymphony.xwork2.validator.validators.RegexFieldValidator" />
validators >
<Table style = "BORDER-RIGHT: # f0c000 1px solid; BORDER-TOP: # f0c000 1px solid; MARGIN-TOP: 8px; MARGIN-BOTTOM: 8px; BORDER-LEFT: # f0c000 1px solid; BORDER-BOTTOM: # f0c000 1px solid; BACKGROUND-COLOR: # ffffce; TEXT-ALIGN: left "cellPadding cellSpacing = 0 = 0 border = 0> <tr> <td style =" PADDING-RIGHT: 4px; PADDING-LEFT: 4px; PADDING-BOTTOM: 4px; PADDING-TOP: 4px "> </ td> <td style="PADDING-RIGHT: 4px; PADDING-LEFT: 4px; PADDING-BOTTOM: 4px; PADDING-TOP: 4px; FONT-FAMILY:åŽæ–‡ä»¿å®‹"> Each validator on the specific usage, please refer to the following link:
Http://wiki.javascud.org/display/ww2cndoc/Validation
The links are some very information, please read carefully. </ Td> </ tr> </ table> Aggregate
Struts small anonymity of the Simple Message
* Small confusing the message simple Struts
* Copyright (C) 2004 Xmihu (webmaster@javafan.net)
* Code-writing: small confused
* Update: 2004.09.22
*
* The code for the free code, you can be transmitted, modified, including IMPRINT
* Information.
* This procedure only learn how to use Struts entry, I am also beginners, and we hope that a
* From learning together.
##################################################
1. Development environment:
OS: WindowsXP / RedHat Linux 8.0
Development Tools: Eclipse2.1.3 + EasyStruts
Compiler environment: j2sdk1.4.2 Struts + 1.1
Service Engine: Resin2.1.8/Tomcat5
Database: MySQL4.0.14-nt
Database-driven: mm.mysql-2.0.4-bin.jar
Note: The above javafan tools and kits are available for download (except Eclipse)
2. Database
To the establishment of a database known as gb, and then directly to the database directory can be imported under the gb.sql.
Mysql-u root-p gb
3. After decompression will be compressed or Resin on the Tomcat webapps directory, or Resin start Tomcat,
Http://localhost:8080/strutsgb visit to run.
4 Contact
Website: http://www.javafan.net
Forum: http://www.javafan.net/jive
Email: webmaster@javafan.net
Oicq: 40917925 function TempSave (ElementID) (CommentsPersistDiv.setAttribute ( "CommentContent" document.getElementById (ElementID). Value); CommentsPersistDiv.save ( "CommentXMLStore");) function Restore (ElementID) (CommentsPersistDiv.load ( "CommentXMLStore" ); document.getElementById (ElementID). CommentsPersistDiv.getAttribute value = ( "CommentContent");) </ td> <td width="188" valign="top" class="ArticleTeitle">
</ Td> </ tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle">
Support multi-client client / server programming
Client procedures and the above procedure is exactly the same, if a reader would read very carefully the above procedures, you can skip reading is not the main focus on the Server end of the procedure.
1. Client programs: MultiTalkClient.java
Import java.io. *;
Import java.net .*;
(Public class MultiTalkClient
Public static void main (String args []) (
Try (
Socket socket = new Socket ( "127.0.0.1", 4700);
/ / To the port of the 4700 issue of customer requests BufferedReader sin = new BufferedReader (new InputStreamReader (System.in));
/ / From the standard input device structure BufferedReader object PrintWriter os = new PrintWriter (socket.getOutputStream ());
/ / Get output from the current Socket object, and object BufferedReader is constructed PrintWriter = new BufferedReader (new InputStreamReader (socket.getInputStream ()));
/ / Get input from Socket object flow, structure and the corresponding object BufferedReader String readline;
= Sin.readLine readline () / / read from standard input into a string while (! Readline.equals ( "bye")) (
/ / If read from standard input string is "bye" to stop cycle os.println (readline);
/ / Read from standard input into the system's output to a string Server
Os.flush ();
/ / Set output streams, Server immediately before the string System.out.println ( "Client:" + readline);
/ / In the standard print output read string System.out.println ( "Server:" + is.readLine ());
/ / Server read a string, and print to the standard output = sin.readLine readline ();
/ / Read from standard input into a string) / / cycle os.close () / / output streams closed Socket is.close () / / close Socket input streams socket.close () / / close Socket
) Catch (Exception e) (
System.out.println ( "Error" + e) / / error, then print an error message)
)
)
2. Server-side procedures: MultiTalkServer.java
Import java.io. *;
Import java.net .*;
Import ServerThread;
(Public class MultiTalkServer
Static int clientnum = 0; / / static member variable, the current record, the number of public static void main (String args []) throws IOException (
ServerSocket serverSocket = null;
Boolean listening = true;
Try (
ServerSocket = new ServerSocket (4700);
/ / Create a ServerSocket 4700 in the port monitor customer requests) catch (IOException e) (
System.out.println ( "Could not listen on port: 4700.");
/ / Error, print an error message System.exit (-1) / / exit)
While (listening) (/ / cycle forever eavesdropping new ServerThread (serverSocket.accept (), clientnum). Start ();
/ / Listening to customer requests, according to The Socket object creation and customer service thread count, and start clientnum + +; / / increase customer count)
ServerSocket.close () / / close ServerSocket
)
)
3. Procedures ServerThread.java
Import java.io. *;
Import java.net .*;
Public class ServerThread extends Thread (
Socket socket = null; / / thread associated with the preservation of Socket object int clientnum; / / preservation of the counting process, public ServerThread (Socket socket, int num) (/ / constructor function this.socket = socket; / / Initial of the socket variables clientnum +1 = num; / /) variable initialization clientnum
Public void run () (/ / try (main thread
String line;
BufferedReader is = new BufferedReader (new InputStreamReader (socket.getInputStream ()));
/ / Get input from Socket object flow, structure and the corresponding object BufferedReader newPrintWriter PrintWriter os = (socket.getOutputStream ());
/ / Get output from the current Socket object, and object structure PrintWriter BufferedReader sin = new BufferedReader (new InputStreamReader (System.in));
/ / From the standard input device structure BufferedReader object System.out.println ( "Client:" + + clientnum is.readLine ());
/ / In the standard print output from the client read the string line = sin.readLine ();
/ / Read from standard input into a string while (! Line.equals ( "bye")) (
/ / If the string is "bye," then stop cycle os.println (line);
/ / Output to the client string os.flush ();
/ / Set the output flow, so that immediately received the Client string System.out.println ( "Server:" + line);
/ / Standard output on the system to print the string System.out.println ( "Client:" + + clientnum is.readLine ());
/ / Client read a string, and print on the line to the standard output = sin.readLine ();
/ / Read from standard input into a string) / / cycle os.close () / / output streams closed Socket is.close () / / close Socket input streams socket.close () / / close Socket
Server.close () / / close ServerSocket
) Catch (Exception e) (
System.out.println ( "Error:" + e);
/ / Error, printing error messages)
)
)
↑ Back Another tomcat web application under the build.xml file
Build file for the JCatalog Project.
Derek Y. Shen
6 / 2004
</ Td> <td width="171" valign="top" class="ArticleTeitle">
</ Td> </ tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle">