<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="LoginServlet" method="post">
username:<input type="text" name="username"/><br/>
password:<input type="password" name="password"/><br/>
<input type="submit" value="login"/>
</form>
</body>
</html>
main.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>This is the main page.</h1>
</body>
</html>
package http.client;
import http.Utils;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
public class RequestMainClient {
@SuppressWarnings("deprecation")
public static void main(String[] args)throws Exception {
HttpClient httpclient = new DefaultHttpClient();
/* execute login operation */
// prepare the request url
HttpPost httpPost = new HttpPost("http://localhost:7070/HttpServer/LoginServlet");
// prepare the request parameters
List<NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username","admin"));
params.add(new BasicNameValuePair("password","admin"));
// set the request entity
httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
System.out.println("executing request " + httpPost.getURI());
HttpResponse response =httpclient.execute(httpPost);
/* request main page */
httpPost.releaseConnection();
httpPost=new HttpPost("http://localhost:7070/HttpServer/main.jsp");
response = httpclient.execute(httpPost);
/* print the result of the request */
Utils.printResponse(response);
// Do not feel like reading the response body
// Call abort on the request object
httpPost.abort();
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
package http.client;
import http.Utils;
import java.io.File;
import java.net.URL;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.DefaultHttpClient;
public class PostXMLClient {
public static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
/* initialize the request method */
// prepare the request url
HttpPost httpPost = new HttpPost("http://localhost:7070/HttpServer/PostXMLServlet");
URL url=PostXMLClient.class.getClassLoader().getResource("http/fruits.xml");
File file=new File(url.getFile());
System.out.println(file.exists());
httpPost.setEntity(new FileEntity(file,ContentType.TEXT_XML));
/* execute operation */
System.out.println("executing request " + httpPost.getURI());
HttpResponse response =httpclient.execute(httpPost);
/* print the result of the request */
Utils.printResponse(response);
// Do not feel like reading the response body
// Call abort on the request object
httpPost.abort();
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}