1. How to make Servlet?
1.1. Make Project on IntelliJ
“Java Enterprise” – “Wep Application” – Next – Done
1.2. Make package
Project – src – Right Click – New – Package
1.3. Make Servlet
Project – src – ‘package(you make 1.2.)’ – Right Click – Servlet
Set Name – ‘OK’
1.4. Set urlPatterns
The ‘urlPatterns’ value is not included by default. So you have to make it.
package com.waterhouse.sample; 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 = "HelloServlet", urlPatterns = {"URL Path(you can make more path like this -> )","/second_path"}) public class HelloServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<h1>Hello jsp servlet!!!!!!!!!!!!"); out.flush(); } }
line 16-19 is Print out to Browser.
2. Referense URL