3)、 code:
package com.johnny.test; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloWorld extends HttpServlet implements javax.servlet.Servlet{ /* (non-Java-doc) * @see javax.servlet.http.HttpServlet#HttpServlet() */ public HelloWorld() { super(); } /* (non-Java-doc) * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().write("Hello, world 1112!"); } /* (non-Java-doc) * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <servlet> <servlet-name>HelloWorld</servlet-name> <servlet-class>com.johnny.test.HelloWorld</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>/helloworld</url-pattern> </servlet-mapping> </web-app>
5)、 或者右键点击工程,export MyFirstDynamicWebProject.war,然后放到ubuntu的tomcat的webapps目录下面,在浏览器上输入网址:http://localhost:8080/MyFirstDynamicWebProject/helloworld,也会出现Hello, world 1112!
【4】、ubuntu安装tomcat:
1、从http://tomcat.apache.org/download-70.cgi 下载core apache-tomcat-7.0.41.tar.gz
2、确保jdk的路径已经配置好
3、解压运行 sudo ./startup.sh
4、测试:在浏览器中输入http://localhost:8080,出现tomcat网页,则安装成功;
【5】、
public class HelloWorld extends HttpServlet implements javax.servlet.Servlet{ /* (non-Java-doc) * @see javax.servlet.http.HttpServlet#HttpServlet() */ public HelloWorld() { super(); } /* (non-Java-doc) * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ///* StringBuffer xmlInfo = new StringBuffer(); InputStream in = request.getInputStream(); BufferedInputStream buf = new BufferedInputStream(in); byte buffer[] = new byte[1024]; int iRead; while ((iRead = buf.read(buffer)) != -1) xmlInfo.append(new String(buffer, 0, iRead)); buf.close(); String result = ""; String recievxml = xmlInfo.toString();// XMLUtile.filterStringXml(xmlInfo.toString()); System.out.println(recievxml); String para = request.getParameter("user"); System.out.println("usr = "+para); //*/ System.out.println("hello world"); response.getWriter().write("Hello, world !"); } /* (non-Java-doc) * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("hello world doPost"); doGet(request,response); response.getWriter().write("Hello, world !"); } /* public String getMethod(String xmlInfo) throws DocumentException { String method = ""; try { SAXReader reader = new SAXReader(); Document doc = reader.read(new ByteArrayInputStream(xmlInfo .getBytes())); Element rootElement = doc.getRootElement(); method = rootElement.attributeValue("method"); } catch (Exception e) { e.printStackTrace(); } return method; } */ }
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>dongle settings</title> <script type="text/javascript" src="./Global/jquery.min.js"></script> <script type="text/javascript" src="./Global/jquery.json.min.js"></script> <script language="javascript"> $(window).load(function(){ $.post("http:localhost:8080/MyFirstDynamicWebProject/helloworld", $.toJSON({method : "test_load",para:"test"}), function(data) { var result = $.parseJSON(data); console.log("test post sucess!"); console.log(result); }); }); function test_onclick(){ $.post("http:localhost:8080/MyFirstDynamicWebProject/helloworld", $.toJSON({method : "test_button",para:"test button"}), function(data) { var result = $.parseJSON(data); console.log("test post sucess!"); console.log(result); }); } </script> </head> <body> <p>servlet test</p> <p>This is a test html</p> <hr> <br> <input type="button" value="测试" onclick="test_onclick()" id="test_button"/> </form> </body> </html>