Intellij Idea创建的第一个JavaWeb程序
第一个JavaWeb程序
1. 创建
2. 目录结构
src目录用于放java源文件,web目录用于放web资源,WEB-INF是Java web应用固定的存放配置及类库的目录,index.jsp是我们首页,JavaWeb.iml是IntelliJ的项目文件,打开工程就是打开它 了,External Library是一些外部引用的库,展开看看好多。
3. 创建Servlet
修改代码
package com.myweb;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
/**
* Created by Jiqing on 2017/1/9.
*/
@WebServlet(name = "SayHello")
public class SayHello extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hello World!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>");
out.println("</html>");
}
}
4.配置
<?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_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>SayHello</servlet-name>
<servlet-class>com.myweb.SayHello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SayHello</servlet-name>
<url-pattern>/sayhello</url-pattern>
</servlet-mapping>
</web-app>