Java应用程序与Web服务器交互

时间:2022-09-17 20:49:41

1、服务端的代码如下(需要javax.servlet.jar):

package com.lanp.servlet;

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 HandlerHttpRequestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1)throws ServletException, IOException {
		super.doGet(arg0, arg1);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String username =req.getParameter("username");          //获取username所对应的value
        String password =req.getParameter("password");           //获取password所对应的value
        System.out.println("The received username and password is: " + username + "/" +password);
        
        // 向请求端发回反馈信息
        PrintWriter out =resp.getWriter();
        out.print("OK");
        out.flush();
        out.close();
        
        super.doPost(req, resp);
	}
}

 

2、在Tomcat的webapps文件夹下新建一个JavaApp的文件夹,在JavaApp下新建一个WEB-INF文件夹,在WEB-INF下新建lib文件夹和classes文件夹,在WEB-INF下新建一个web.xml文件。

3、lib文件夹下放commons-logging-1.0.4.jar和commons-logging-1.0.4.jar。

4、classes文件夹下放HandlerHttpRequestServlet.class文件。

5、web.xml文件的内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>JavaApp</display-name>
	
	 <servlet>
         <servlet-name>JavaAppAervletUpLoad</servlet-name>
          <servlet-class>com.lanp.servlet.HandlerHttpRequestServlet</servlet-class>
    </servlet>
 
    <servlet-mapping>   
         <servlet-name>JavaAppAervletUpLoad</servlet-name>
         <url-pattern>/upload</url-pattern>  
    </servlet-mapping>
	
	
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>


6、Java应用程序代码如下:

package com.lanp;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Java应用程序
 * @author LanP
 * @since 2011-11-1 22:36
 * @version V1.0
 */
public class JavaApp {

	public static void main(String[] args) throws Exception {
		// 服务地址
        URL url = new URL("http://127.0.0.1:8080/JavaApp/upload");

        // 设定连接的相关参数
        HttpURLConnection connection= (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
        
        // 向服务端发送key = value对
        out.write("username=lanp&password=lanpiao");
        out.flush();
        out.close();
        
        // 获取服务端的反馈
        String strLine="";
        String strResponse ="";
        InputStream in =connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        while((strLine =reader.readLine()) != null)
        {
                 strResponse +=strLine +"\n";
        }
        System.out.print(strResponse);
	}

}


7、先启动Tomcat后运行JavaApp。

 

OK,TKS!