Java - general

Servlets

Servlets are carried out by a Java Virtual Machine inside of the web-server. So, they are the companion piece of applets which are carried out by a Java Virtual Machine inside of the web-server.



Servlets can, like Java-applications, access local files and programs on the web-server. So, they are not subject to the limits of applets.

The following minimal Servlet returns the handed over parameters again. If it waits no applet on the Client, the returned signs are simply depicted in the web-browser. If an applet recieves the data, it can, of course, process the data.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
 
public class MyServlet extends HttpServlet {
   String strParam1, strParam2;
 
   public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 
      String strParam1 = req.getParameter("parm1");
      String strParam2 = req.getParameter("parm2");
 
      resp.setContentType("text/html");
      ServletOutputStream out = resp.getOutputStream();
      out.println ("The value for parameter1 was: "+strParam1);
      out.println ("The value for parameter2 was: "+strParam2);
      out.flush();
      out.close();
  }
 
}

The call directly of a browser would look like this:
(With it server and port must be replaced with the corresponding values for the Servlet)

http://server:port/servlet/MyServlet?param1=1&param2=2


The call through an applet could look like this:

public void callServlet() {
   String strLine;
   try{
      URL url = new URL("http://server:port/servlet/MyServlet?param1=1&param2=2");
      BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "8859_1"));
 
      while((strLine = in.readLine()) != null){
         System.out.println(strLine);
      }
      in.close();
   }catch (Exception e) {
      e.printStackTrace();
   }
}



Eigene Werkzeuge
Werkzeuge

gratis Counter by GOWEB
seit 9.10.2007