Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.yaon; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StringWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.http.HttpResponse; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.HttpHostConnectException; import org.apache.http.impl.auth.BasicScheme; import org.apache.http.impl.client.DefaultHttpClient; /** * * @author manpreet.singh */ public class NewServlet extends HttpServlet { /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text"); PrintWriter out = response.getWriter(); String ip = request.getParameter("ip"); String port = request.getParameter("port"); String uname = request.getParameter("uname"); String pass = request.getParameter("pass"); StringWriter sw = new StringWriter(); if ("null".equals(ip) && "null".equals(port) && "null".equals(uname) && "null".equals(pass)) { out.println("Null Argument Passed !!"); } else { try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost postRequest = new HttpPost("http://" + ip + ":" + port + "/Yaon/GetInfo"); postRequest.addHeader( BasicScheme.authenticate(new UsernamePasswordCredentials(uname, pass), "UTF-8", false)); HttpResponse res = httpClient.execute(postRequest); if (res.getStatusLine().getStatusCode() == 401) { throw new Exception("Http Authentication Failed !"); } BufferedReader br = new BufferedReader(new InputStreamReader((res.getEntity().getContent()))); String output; System.out.println("Output from Server.... \n"); while ((output = br.readLine()) != null) { System.out.println(output); sw.append(output); sw.append("\n"); } httpClient.getConnectionManager().shutdown(); } catch (HttpHostConnectException e) { out.println(e.getMessage()); } catch (Exception e) { out.println(e.getMessage()); } out.println(sw.toString()); } } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP <code>POST</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold> }