New-->Project 2.第二步 1.找到Java Enterprise之后,在Application Sever中找到..."/>
您好,登錄后才能下訂單哦!
我以2018版的intellij idea為例
一.創(chuàng)建JavaWeb
1.第一步
點擊左上角的File-->New-->Project
2.第二步
1.找到Java Enterprise之后,在Application Sever中找到自己的Tomcat,同時勾選中Web Application
2.第一項是你的工程名字,隨便取啦,第二個是你的工程路徑,選擇好之后點擊finish
3.創(chuàng)建classes和lib文件夾,名字不可改
像這樣
3.第三步
1.在File中找到Project Structure
2.點擊Modules ,選擇Paths,選中Use module compile output path ,把路徑改成剛剛創(chuàng)建的classes文件夾
3.ok后再點旁邊的Dependencies,點擊"+"號,選擇1 JARs or directories ,
4,選擇你剛剛創(chuàng)建的lib文件夾,OK
5.選中jar Directory。
4.第四步
1.回到主界面后,點擊Run ,選擇Edit Configurations
2.看tomcat已經(jīng)有啦,這是java Enterprise的好處啦,點擊Deployment ,
3.點擊右上角的三角形
4.運行成功。
至此,intellij idea創(chuàng)建并設置javaweb工程全部完成,下面是在其中編寫并運行Servlet。
二.創(chuàng)建Servlet
1.先下src文件加下創(chuàng)建一個servlet Hellow.java文件
2.對Hello.java進行編譯
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.IOException; import java.io.PrintWriter; @WebServlet(name = "Hello") public class Hello extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //設置響應內(nèi)容類型 response.setContentType("text/html"); //設置邏輯實現(xiàn) PrintWriter out = response.getWriter(); out.println("<h2>Servlet后臺</h2>"); } }
3.編寫web.xml配置文件
<?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>Hello</servlet-name> <servlet-class>Hello</servlet-class> </servlet> <servlet-mapping> <servlet-name>Hello</servlet-name> <url-pattern>/Hello</url-pattern> </servlet-mapping> </web-app>
4.運行Servlet,發(fā)現(xiàn)在classes目錄下會生成Hello.class文件
5.重新編寫index.jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <a href="Hello" rel="external nofollow" >第一個動態(tài)web項目</a> </body> </html>
6.完成
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。