/MyTestServlet
package com.lingzhuo.test;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MyTestServlet
*/
@WebServlet("/MyTestServlet")
public class MyTestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MyTestServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userName = request.getParameter("user_name");
String password = request.getParameter("password");
System.out.println("用户名:" + userName);
System.out.println("密 码:" + password);
String s = "";
Connection conn = SQLManager.newInstance().getConnection();
try {
PreparedStatement state = conn.prepareStatement("select * from user where user_name=? and password=?");
state.setString(1, userName);
state.setString(2, password);
ResultSet set = state.executeQuery();
set.last();
int num = set.getRow();
if (num == 1) {
s = "登陆成功";
} else {
s = "用户名或密码错误";
}
} catch (SQLException e) {
e.printStackTrace();
}
response.setHeader("Content-type", "text/html;charset=UTF-8");
response.getWriter().append(userName+password);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
response.getWriter().append(s);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)//请求 推送
throws ServletException, IOException {
doGet(request, response);
}
}
Encoding
package com.lingzhuo.test;
import java.io.UnsupportedEncodingException;
public class Encoding {
public static String doEncodign(String string)
{
try {
byte[] array=string.getBytes("ISO-8859-1");
string = new String(array, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return string;
}
}
HTTPURLConnectionDoGet
package com.lingzhuo.test;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.awt.event.ActionEvent;
public class HTTPURLConnectionDoGet extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
HTTPURLConnectionDoGet frame = new HTTPURLConnectionDoGet();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public HTTPURLConnectionDoGet() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnDoget = new JButton("DoGet");
btnDoget.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String urlString = "http://localhost:8080/MyServiceTest/MyTestServlet?user_name=zhangsan&password=123456";
try {
URL url = new URL(urlString);
URLConnection connect = url.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) connect;
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.setReadTimeout(30000);
httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
int code = httpURLConnection.getResponseCode();
System.out.println("HTTP的状态码是:" + code);
if (code == httpURLConnection.HTTP_OK) {
InputStream is=httpURLConnection.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String theLine = br.readLine();
while (theLine != null) {
System.out.println(theLine);
theLine = br.readLine();
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
catch (SocketTimeoutException e) {
System.out.println("网络连接超时");
} catch (ConnectException e) {
System.out.println("连接出现异常");
} catch (IOException e) {
e.printStackTrace();
}
}
});
btnDoget.setBounds(145, 116, 93, 67);
contentPane.add(btnDoget);
}
}
HTTPURLConnectionDoPost
package com.lingzhuo.test;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class HTTPURLConnectionDoPost extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
HTTPURLConnectionDoPost frame = new HTTPURLConnectionDoPost();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public HTTPURLConnectionDoPost() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("DoPost测试");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String urlString="http://localhost:8080/MyServiceTest/MyTestServlet";
try {
URL url = new URL(urlString);
URLConnection connection=url.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
String params="user_name=zhangsan&password=1234546";
httpURLConnection.getOutputStream().write(params.getBytes());
int code=httpURLConnection.getResponseCode();
try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println("HTTP的状态码是:"+code);
if(code==httpURLConnection.HTTP_OK)
{
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
e.printStackTrace();
}
InputStream is=httpURLConnection.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String line=br.readLine();
while(line!=null)
{
System.out.println(line);
line=br.readLine();
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
; }
});
btnNewButton.setBounds(146, 117, 130, 76);
contentPane.add(btnNewButton);
}
}
SQLManager
package com.lingzhuo.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class SQLManager {
private Connection connection;
private static SQLManager manager;
public Connection getConnection() {
return connection;
}
public static synchronized SQLManager newInstance() {
if (manager == null) {
manager = new SQLManager();
}
return manager;
}
private SQLManager() {
{
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/clazz";
String user = "root";
String password = "201216328";
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
DoPostTest
package com.lingzhuo.test;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.awt.event.ActionEvent;
public class DoPostTest extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DoPostTest frame = new DoPostTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public DoPostTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 784, 635);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String urlString = "http://localhost:8080/MyServiceTest/MyTestServlet";
try {
URL url = new URL(urlString);
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setConnectTimeout(30000);
httpConnection.setReadTimeout(30000);
httpConnection.setRequestProperty("Accept-Charset", "utf-8");
httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpConnection.setRequestMethod("POST");
httpConnection.setDoOutput(true);
httpConnection.setUseCaches(false);
String params="user_name=zhangsan&password=123456";
httpConnection.getOutputStream().write(params.getBytes());
System.out.println("-------------------");
int code=httpConnection.getResponseCode();
System.out.println(code);
if(code==HttpURLConnection.HTTP_OK){
InputStream is=httpConnection.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String line=br.readLine();
while(line!=null){
System.out.println(line);
line=br.readLine();
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
btnNewButton.setBounds(228, 130, 293, 278);
contentPane.add(btnNewButton);
}
}
HttpClientDoGet
package com.lingzhuo.test;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.util.concurrent.TimeUnit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.fluent.Response;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
public class HttpClientDoGet extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
HttpClientDoGet frame = new HttpClientDoGet();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public HttpClientDoGet() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 683, 516);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("HttpClientDoGet");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String urlString="http://localhost:8080/MyServiceTest/MyTestServlet?user_name=zhangsan&password=123456";
HttpClientBuilder builder=HttpClientBuilder.create();
builder.setConnectionTimeToLive(3000, TimeUnit.MILLISECONDS);
HttpClient client=builder.build();
HttpGet get=new HttpGet(urlString);
get.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
try {
System.out.println(2);
HttpResponse response=client.execute(get);
System.out.println("-------------");
StatusLine statusLine=response.getStatusLine();
int code=statusLine.getStatusCode();
System.out.println(code);
if(code==HttpURLConnection.HTTP_OK){
HttpEntity entity=response.getEntity();
InputStream is=entity.getContent();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String line=br.readLine();
while(line!=null){
System.out.println(line);
line=br.readLine();
}
}
} catch (ClientProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
btnNewButton.setBounds(221, 150, 218, 169);
contentPane.add(btnNewButton);
}
}
#
package com.lingzhuo.test;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
public class HttpClientDoPost extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
HttpClientDoPost frame = new HttpClientDoPost();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public HttpClientDoPost() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 810, 665);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("HttpClientDoPost");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String url="http://192.168.0.52:8080/MyServiceTest/MyTestServlet";
HttpClientBuilder builder=HttpClientBuilder.create();
builder.setConnectionTimeToLive(3000, TimeUnit.MILLISECONDS);
HttpClient client=builder.build();
HttpPost post=new HttpPost(url);
NameValuePair pair1=new BasicNameValuePair("user_name", "张三");
NameValuePair pair2=new BasicNameValuePair("password", "123456");
ArrayList<NameValuePair> params=new ArrayList<>();
params.add(pair1);
params.add(pair2);
try {
post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
post.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
System.out.println(2);
HttpResponse response=client.execute(post);
System.out.println("--------------");
int code=response.getStatusLine().getStatusCode();
System.out.println(code);
if(code==200){
HttpEntity entity=response.getEntity();
InputStream is=entity.getContent();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String line=br.readLine();
while(line!=null){
System.out.println(line);
line=br.readLine();
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
btnNewButton.setBounds(193, 162, 305, 199);
contentPane.add(btnNewButton);
}
}