1. 程式人生 > >搭建和啟動javaWeb專案

搭建和啟動javaWeb專案

首先,我們得配置伺服器,我的demo採用tomcat 你只要找到tomcat的home路徑就好了,後面會自動給你提示的 注意這個圖示,你需要打上勾勾,標識著這是一個網站專案 接著next,  起名     修改index.jsp檔案的內容
<%--
Created by IntelliJ IDEA.
User: wanglei
Date: 2018/12/27
Time: 1:36
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8"
language="java" %> <html> <head> <title>HelloWorld</title> </head> <body> <h1>你好,世界!</h1> </body> </html>

 

 

如果正常,你點選一下這個小箭頭   會自動跳轉到瀏覽器訪問   使用Servlet建立一個服務 DemoServlet.java
 1
package com.lsh; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.annotation.WebServlet; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import java.io.IOException; 9 10 @WebServlet("/demo")
11 public class DemoServlet extends HttpServlet { 12 @Override 13 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 14 System.out.println("DemoServlet.service"); 15 16 //請求轉發到index.jsp 17 req.getRequestDispatcher("/main.jsp").forward(req,resp); 18 19 } 20 }

 

main.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>你好,逍遙!</h1>
</body>
</html>

 

目錄結構 瀏覽器的響應結果  

  作者:愛學習的小磊 

  出處: https://www.cnblogs.com/WangLei2018/      本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線,否則保留追究法律責任的權利。