📄 realtimeservlet.java
字号:
package com.idmsoft.netflow.realtime;
// import necessary java packages
import java.io.*;
import java.util.*;
import java.text.*;
// import necessary servlet packages
import javax.servlet.*;
import javax.servlet.http.*;
// Class: ServletApplet
// Description: This class inherits from HttpServlet in order to aquire
// servlet properties. When first called, it will return the
// applet code that creates an applet for the user to enter
// values. Then, whenever the user presses the compute results
// button, the doPost method will be called and it will talk
// to the applet through a URL Connection, returning a random
// rate value so the applet can finish its computation.
//
public class RealtimeServlet extends HttpServlet
{
// Method: doGet
// Parameters: HttpServletRequest, HttpServletResponse
// Return type: void
// Description: This method is first called when the initial servlet
// URL is specified. It will return HTML that creates
// an applet for the user to interact with.
//
public void doGet( HttpServletRequest req, HttpServletResponse resp )
throws ServletException, IOException {
resp.setContentType( "text/html" ); // Specify that HTML will be returned
PrintWriter out = resp.getWriter(); // Create the writer
// Write out the HTML that creates the interest applet
out.println( "<HTML>" );
out.println( "<HEAD>" );
out.println( "<TITLE>Testing Realtime Traffic Monitor</TITLE>" );
out.println( "</HEAD>" );
out.println( "<BODY>" );
out.println( "<H1 ALIGN=\"CENTER\">Realtime Traffic Monitor</H1>" );
out.println( "<P>" );
out.println( "<APPLET CODE=\"com.idmsoft.netflow.realtime.RealtimeTrafficApplet.class\" CODEBASE=\"/netflow/applet/\" archive=\"trafficapplet.jar\" WIDTH=600 HEIGHT=380 >" );
out.println( "<param name=\"counters\" value=\"2\">");
out.println( "<param name=\"counter0\" value=\"Load average 1 min\">");
out.println( "<param name=\"counter1\" value=\"Load average 5 min\">");
out.println( "<param name=\"color0\" value=\"#00FF00\" >");
out.println( "<param name=\"color1\" value=\"#0000FF\" >");
out.println( "<param name=\"tick\" value=\"6\" >");
out.println( "<param name=\"showLegend\" value=\"0\">");
out.println( "<param name=\"showBorder\" value=\"1\">");
out.println( "<param name=\"backgroundColor\" value=\"#FFFFFF\">");
out.println( "<param name=\"gridColor\" value=\"#000000\">");
out.println( "</APPLET>" );
out.println( "</BODY>" );
out.println( "</HTML>" );
}
// Method: doPost
// Parameters: HttpServletRequest, HttpServletResponse
// Return type: void
// Description: This class is called when the a URLConnection specifies
// this servlet. It will set its response to send back plain
// text to the applet. First, it will open a reader to read
// in the applet's input. Then, it will parse this input
// into attributes and their values. It will then check if the rate
// user action was specified and if so, it will write out a
// rate for the applet's URLConnection to read.
//
public void doPost( HttpServletRequest req, HttpServletResponse resp )
throws ServletException, IOException {
resp.setContentType( "text/plain" ); // Return plain text
PrintWriter out = resp.getWriter(); // Create a writer
// Create the reader to read the value from the applet's URLConnection
BufferedReader r = new BufferedReader(
new InputStreamReader(
req.getInputStream() ) );
String str = r.readLine(); // Store the applet's input
r.close(); // Specify the end of use of the reader
// Put the attribute/value pairs of the inputs in a hash table
Hashtable userParams = HttpUtils.parseQueryString( str );
String[] userActions = (String[])userParams.get( "userAction" );
String userAction = userActions[0]; // the first action in the hash table
// If the action is "getRate", then find a rate and write it out
if( userAction.equalsIgnoreCase( "getRate" ) ) {
// Find a random rate between 3.00% and 10.00%
String rate = randomRate();
out.println( rate ); // Send back the rate over the applet's URLConnection
}
else {
out.println( "a" ); // Signifies that "getRate" is not action
}
}
// Method: randomRate
// Parameters: none
// Return type: String
// Description: This method returns a random rate that doesn't repeat.
// It creates an object of the Random class which is seeded
// based on the current time. We then create the rate as a
// double, then turn into a String which is returned.
//
private String randomRate() {
DecimalFormat df = new DecimalFormat( "0.00" ); // Used for number formatting
// Create a Random object
Random rand = new Random();
// Creates a number between 3.00 and 10.00, then converts to a String value
double percent = (rand.nextDouble() * 7) + 3;
String s = String.valueOf( df.format(percent) );
return( s ); // return rate string
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -