Java+Tomcat+Servlet實現頁面訪問

來源:互聯網
上載者:User

標籤:enc   hello   protect   exception   iter   RoCE   xdp   安裝   start   

window7下Java環境安裝記錄:
一、安裝Tomcat
1、下載tomcat 7.0,解壓,無需安裝,放置到目錄:D:\apache-tomcat-7.0.90。
2、配置系統內容變數,CATALINA_BASE=D:\apache-tomcat-7.0.90,CATALINA_HOME=D:\apache-tomcat-7.0.90,在Path中新增“%CATALINA_HOME%\lib;%CATALINA_HOME%\bin”的環境變數。
3、進入D:\apache-tomcat-7.0.90\bin,執行startup啟動tomcat,瀏覽器中輸入“127.0.0.1:8080”即可查看是否成功。
二、搭建Servlet,使用Servlet實現動態web
1、使用Idea建立web Application project.

2、在src目錄下new 一個servlet,起名:test.servlet.study

3、重寫Servlet類裡的doPost和doGet方法。

package test.servlet.study;import java.io.IOException;import java.io.PrintWriter;public class Servlet extends javax.servlet.http.HttpServlet {    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {        PrintWriter out = response.getWriter();        out.println("POST:Hello world!" + request.getParameter("name"));    }    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {        response.setCharacterEncoding("GBK");        PrintWriter out = response.getWriter();        String sss = show(request.getParameter("name"));        out.println("GET:Hello world!" + sss);    }    public String show(String name) {        return name + ",你好!!!";    }}

4、修改web.xml增加servlet-mapping標籤,增加rul地址->/servlet/test/hello。

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"         version="4.0">    <servlet>        <servlet-name>Servlet</servlet-name>        <servlet-class>test.servlet.study.Servlet</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>Servlet</servlet-name>        <url-pattern>/servlet/test/hello</url-pattern>    </servlet-mapping></web-app>

5、打war包Build-->Build Artifacts,包名webAppTest1,放到D:\apache-tomcat-7.0.90\webapps目錄下。
6、重啟tomcat,訪問http://127.0.0.1:8080/webAppTest1/servlet/test/hello?name=jien即可。

參考:
使用Servlet:https://www.cnblogs.com/xdp-gacl/p/3760336.html
打包:79404911

Java+Tomcat+Servlet實現頁面訪問

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.