搭建和启动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>
目录结构
浏览器的响应结果
 

  作者:爱学习的小磊 

  
  本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
 

猜你喜欢

转载自www.cnblogs.com/WangLei2018/p/10182859.html