您的位置:首页 > 运维架构

DoGet和DoPost

2015-08-12 14:35 288 查看
给serverlet传输数据

serverlet的编写

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

String username=request.getParameter("username");//得到的网页的uername,
String password=request.getParameter("password");
username=Encoding.encoding(username);//通过编写的方法,进行转换
System.out.println("用户名"+username+"\n"+"密码"+password);
/**
* 然后在给浏览器数据
*/
response.setHeader("content-type","text/html;charset=utf-8");//让浏览器以utf-8格式解析
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//返回给消息提供者的消息
response.getWriter().append("用户名:"+username);
response.getWriter().println("<br>");
response.getWriter().append("密码    :"+password);
}


public class Encoding {
public static String encoding(String s) {

try {
byte[] array = s.getBytes("ISO-8859-1");
s = new String(array, "utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return s;
}
}


使用get方法传输

String urlTemp = "http://localhost:8080/TestWeb/TestServerlet?username=zhangsan&password=123";
try {
URL url = new URL(urlTemp);// 建立一个新连接
URLConnection con = url.openConnection();// 打开连接
HttpURLConnection urlcon = (HttpURLConnection) con;// 连接转化为Http
urlcon.setRequestMethod("GET");// 设置服务器的方法
urlcon.setReadTimeout(3000);//设置读取超时
urlcon.setConnectTimeout(3000);//连接超时
urlcon.setRequestProperty("Accept-Charset", "utf-8");
// 设置可以接受序列化的java对象
urlcon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
int code = urlcon.getResponseCode();
System.out.println("状态码:" + code);
if (code == HttpURLConnection.HTTP_OK) {
InputStream is = urlcon.getInputStream();
BufferedReader bRead = new BufferedReader(new InputStreamReader(is));
String s = bRead.readLine();
while (s != null) {
System.out.println(s);
s = bRead.readLine();
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SocketTimeoutException e) {
System.out.println("连接超时");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


使用post的方法传输

public void actionPerformed(ActionEvent arg0) {
String urlTemp = "http://localhost:8080/TestWeb/TestServerlet";
try {
URL url = new URL(urlTemp);
URLConnection con = url.openConnection();
HttpURLConnection httpCon = (HttpURLConnection) con;
httpCon.setRequestMethod("POST");// 设置服务器的方法
httpCon.setReadTimeout(30000);// 设置读取超时
httpCon.setConnectTimeout(3000);// 连接超时
httpCon.setRequestProperty("Accept-Charset", "utf-8");
// 设置可以接受序列化的java对象
httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpCon.setDoOutput(true);//设置post输出为真post方法必须设置
httpCon.setUseCaches(false);//设置缓存为false,post方法必须设置的
String tempSend="username=zhangsan&password=123";
httpCon.getOutputStream().write(tempSend.getBytes());
int code =httpCon.getResponseCode();
System.out.println(code);
if (code == HttpURLConnection.HTTP_OK) {
InputStream is = httpCon.getInputStream();
BufferedReader bRead = new BufferedReader(new InputStreamReader(is));
String s = bRead.readLine();
while (s != null) {

4000
System.out.println(s);
s = bRead.readLine();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  serverlet