Java/Servlets/Client

Материал из Java эксперт
Перейти к: навигация, поиск

Client Post

 
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.rumons.httpclient.HttpClient;
import org.apache.rumons.httpclient.HttpStatus;
import org.apache.rumons.httpclient.methods.PostMethod;
import org.apache.rumons.httpclient.NameValuePair;
public class ClientPost extends HttpServlet {
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {
    HttpClient httpClient = new HttpClient();
    PostMethod postMethod = new PostMethod(
        "http://localhost:8080/home/viewPost.jsp");
    NameValuePair[] postData = { new NameValuePair("username", "devgal"),
        new NameValuePair("department", "development"),
        new NameValuePair("email", "devgal@yahoo.ru") };
    //the 2.0 beta1 version has a
    // PostMethod.setRequestBody(NameValuePair[])
    //method, as addParameters is deprecated
    postMethod.addParameters(postData);
    httpClient.executeMethod(postMethod);
    //display the response to the POST method
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    //A "200 OK" HTTP Status Code
    if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
      out.println(postMethod.getResponseBodyAsString());
    } else {
      out.println("The POST action raised an error: "
          + postMethod.getStatusLine());
    }
    //release the connection used by the method
    postMethod.releaseConnection();
  }
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {
    doPost(request, response);
  }
}





Client Pull

 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ClientPull extends HttpServlet {
  public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();
    res.setHeader("Refresh", "10");
    out.println(new Date().toString());
  }
}





Client Pull and Move

 
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ClientPullMove extends HttpServlet {
  static final String NEW_HOST = "http://www.jexp.ru";
  public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    String newLocation = NEW_HOST + req.getRequestURI();
    res.setHeader("Refresh", "10; URL=" + newLocation);
    out.println("The requested URI has been moved to a different host.<BR>");
    out.println("Its new location is " + newLocation + "<BR>");
    out.println("Your browser will take you there in 10 seconds.");
  }
}





Get Users IP Address

 
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Main extends HttpServlet {
  protected void service(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    // Print out the IP address of the caller
    out.println(request.getRemoteAddr());
  }
}