jsp教程3-Eclipse 創(chuàng)建項目

將 Tomcat 和 Eclipse 相關(guān)聯(lián)

Eclipse J2EE下載后,解壓即可使用,我們打開Java EE ,選擇菜單欄Windows-->preferences(Mac 系統(tǒng)為 Eclipse-->偏好設(shè)置),彈出如下界面:

Paste_Image.png

上圖中,點擊"add"的添加按鈕,彈出如下界面:

Paste_Image.png

在選項中,我們選擇對應(yīng)的 Tomcat 版本,接著點擊 "Next",選擇 Tomcat 的安裝目錄,并選擇我們安裝的 Java 環(huán)境:

Paste_Image.png

點擊 "Finish",完成配置。
創(chuàng)建實例
選擇 "File-->New-->Dynamic Web Project",創(chuàng)建 TomcatTest 項目:

Paste_Image.png
Paste_Image.png
Paste_Image.png

注意如果已默認選擇了我們之前安裝的 Tomcat 和 JDK 則可跳過此步。
然后,單擊finish, 繼續(xù):

Paste_Image.png
Paste_Image.png
Paste_Image.png

上圖中各個目錄解析:
deployment descriptor:部署的描述。
Web App Libraries:自己加的包可以放在里面。
build:放入編譯之后的文件。
WebContent:放進寫入的頁面。
在WebContent文件夾下新建一個test.jsp文件。在下圖中可以看到它的默認代碼:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>

接著我們修改下test.jsp文件代碼如下所示:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>南城的人</title>
</head>
<body>
<%
  out.println("Hello World!");
%>
</body>
</html>
Paste_Image.png

![Upload Paste_Image.png failed. Please try again.]

瀏覽器訪問 http://localhost:8080/TomcatTest/test.jsp, 即可輸出正常結(jié)果

Servlet 實例創(chuàng)建

我們也可以使用以上環(huán)境創(chuàng)建 Servlet 文件,選擇 "File-->New-->Servlet":

Paste_Image.png
Paste_Image.png
package com.runoob.test;

import java.io.IOException;
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 HelloServlet
 */
//@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
//      使用GBK設(shè)置中文正常顯示
    response.setCharacterEncoding("GBK");
    response.getWriter().write("南城:http:、、www.zonzzz.com");
    
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
}

}

創(chuàng)建 /TomcatTest/WebContent/WEB-INF/web.xml 文件(如果沒有),代碼如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!-- 配置和映射servlet -->
<servlet>
    <!-- servlet 注冊的名字 -->
    <servlet-name>HelloServlet</servlet-name>
    <!-- setvlet 的全類名 -->
    <servlet-class>com.runoob.test.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
    <!-- 需要個某一個servlet子節(jié)點的servlet-name子節(jié)點的文本節(jié)點保持一致 -->
    <servlet-name>HelloServlet</servlet-name>
    <!-- 映射的具體訪問路徑:/代表當前WEB應(yīng)用的根目錄 -->
    <url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>

</web-app>
Paste_Image.png
Paste_Image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)自陳明乾的博客,可能有一定更新。 轉(zhuǎn)原文聲明:原創(chuàng)作品,允許轉(zhuǎn)載,轉(zhuǎn)載時請務(wù)必以超鏈接形式標明文章 原始出處 、...
    C86guli閱讀 4,876評論 6 72
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,261評論 6 342
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,638評論 18 399
  • 從三月份找實習(xí)到現(xiàn)在,面了一些公司,掛了不少,但最終還是拿到小米、百度、阿里、京東、新浪、CVTE、樂視家的研發(fā)崗...
    時芥藍閱讀 42,787評論 11 349
  • JSP技術(shù)的強勢: (1)一次編寫,到處運行。在這一點上Java比PHP更出色,除了系統(tǒng)之外,代碼不用做任何更改。...
    leftshine閱讀 2,344評論 1 8

友情鏈接更多精彩內(nèi)容