As a prerequisite, it is assumed that Eclipse has been installed. Eclipse Download This time, I aim to output Hello World using Servlet and JSP in Eclipse.
A Servlet is a program that runs on a web server, that is, on the back end, and is a program written in Java to realize a dynamic web page.
** What is a dynamic web page **? A page that changes the appearance of the page according to the person who accessed it even if the same URL is requested. For example, on an EC site (a site where you can shop online), pages are configured in consideration of your browsing history and information added to your favorites, and pages are provided according to the tastes and behavioral tendencies of each person who visits. .. Servlets are available as one of these methods.
Learning Servlets is good for Java beginners
――A technology that is still very popular today. --Since the web framework hides the Servlet, I rarely write a plain Servlet. Knowledge of Servlet is required when problems occur. --Learn the basic mechanism of web applications
Create a Servlet.
Go through Eclipse's file
→ new
→ other
→ web folder
→ dynamic web project
.
Enter a suitable project name
→ Select the Java version you have for the target runtime
(You can check the java version that can be used from the settings) → Done
Create a Servlet that runs on a web server.
File
→ New
→ Other
→ Web folder
→ Servlet
→ Suitable package name
, Class name
(servlet, SampleTest this time) →Done
Then the following Java file is created.
SampleTest.java
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SampleTest
*/
@WebServlet("/SampleTest")
public class SampleTest extends HttpServlet {
/**
* @see HttpServlet#HttpServlet()
*/
public SampleTest() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
This time we will edit the doGet method.
SampleTest.java
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/view/sample.jsp").forward(request, response);
}
Forwarding is done by the forward () method of the RequestDispatcher instance.
** Forward syntax **
RequestDispatcher dispatcher =
request.getRequestDispatcher("Forward destination");
dispatcher.foward(request,response);
As the forward destination, not only the JSP file but also the Servlet class can be specified.
--For JSP files -Path from / Web-Content --For Servlet class -/ URL pattern
The controller is handled by a Servlet class suitable for receiving requests from users and performing overall control. The output view is handled by a JSP file that specializes in HTML output.
Right-click on WebContent / WEB-INF`` →
folder →
enter the folder name→
done`
view`` →
Other →
JSP file→
Next →
Enter folder name→
Done`
Create JSP file with** Place under WEB-INF
**
When you create a WEB application, the request from the browser is basically the Servlet class. Since the JSP file is created on the assumption that it will be forwarded from the Servlet class and will work, errors and problems may occur when it is called from the browser, so make sure that you cannot make a direct request.
Browsers cannot directly request files located under WEB-INF
.
sample.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
This time I want to output Hello World
, so enter it in the JSP file.
Now that you're ready, let's start the server.
Right-click on SampleTest.java`` Execute
→ Execute on server
Select the server type Tomcat v9.0 Server
This time I used Sevlet to output the basic Hello World Next time, we will use the doPost method to get the parameters.
Recommended Posts