[Java Servlet] Senri no Michi is one step to the second

Let's make a jsp file

First from the template

login.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login screen</title>
</head>
<body>

</body>
</html>

<% @ ~%> on the first line is a promise when writing a jsp file, and other than that, it can be basically treated like HTML. Let's implement inside the body

login.jsp


<form action="/ServletApp/mypage" method="post">
name:<input type="text" name="name"><br>
password:<input type="text" name="password"><br>
<input type="submit" value="login">
</form>

The following is the screen at startup * At the moment, nothing is displayed even if it is started servlet5.png Let's explain in order

Tag name Element name Description
form action Transition destination when the submit element is clicked
It depends on the folder structure, but basically
http://localhost:8080/Project name/action name
method Specify from two types, get and post
get ・ ・ ・ The parameters entered at the end of the transition destination URL will stick together.( /action name?name=hoge&password=Feeling like foo)
post ... does not stick(Those who use this time)
I will write about the points to use properly at a later date
input type="text" Text field
name Key for getting input data on the server side
input type="submit" Button to send the data in the form tag to the server

This is the path specified for the action of the form, Project name = ServletApp I want to transition jsp = mypage.jsp In the case of, specify / ServletApp / mypage

Let's make a Servlet

Create a new java file after creating a suitable package Enter HttpServlet in Superclass and click Browse 2020-05-25.png Select HttpServlet from [Matching items] and click [OK] 2020-05-25 (2).png After creating the class, click [Source]-[Method Override] servlet6.png From Select method to override or implement Select "doGet", "doPost", "destroy", and "init (no arguments)" and click [OK]. 2020-05-25 (4).png This will add the methods used by the Servlet Erase the contents of the method and OK

Method Description Use
doGet This method is called when get is specified in method of form tag of jsp. Main processing to be executed at the time of screen transition
doPost If post is specified for 〃, this method will be called when called.
destroy Processing that works when the servlet is destroyed Log output processing, etc.
init The first process Variable initialization processing, etc.

doGet and doPost will work even if only the user is implemented, but basically both are declared to prevent such things as implementing only doGet even though method = "post" is specified. At this time, describe doXXX (req, res) in the method that is not used. Then it will look like the one below servlet8.png Let's write the contents The one below is mainly used

variable Method Description
res setCharacterEncoding(String charset) Convert the parameter received from jsp to the specified character code
addCookie(Cookie cookie) Set cookies * Explanation at a later date
sendRedirect(String URL) Redirect to the specified URL (transition)
req getParameter(String key) Get the value of the element name specified by name of jsp
getSession Get a session (commentary later)
setAttribute(String key,Object value) Set the element name and value to send to jsp
getRequestDispacher(String jsppath) Set the file path to the jsp you want to send

Click here for specific implementation details servlet10.png

・ 18th line: Set the character code to UTF-8 ・ Lines 19 and 20 By specifying name and password as arguments, respectively

login.jsp


name:<input type="text" name="name"><br>
password:<input type="text" name="password"><br>

You can get the input value from the two text fields of ↑

・ Line 22: If you enter google in the password, move to the Google page ・ Lines 24 and 25: Set the name and value of the element to be passed to the next jsp. (As in line 25, you can set elements that are not in the original jsp regardless) -Line 26: Specify the storage path of the next jsp with an extension Don't forget to connect forward (req, res) at the end

Let's write the transition destination jsp

mypage.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My page</title>
</head>
<body>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%
	String name=request.getParameter("name");
	List<String> list=new ArrayList<>();
	list.add("Hello!");
%>
<%=name%>Mr.<%=list.get(0)%>
</body>
</html>

For the sake of explanation, the code is completely unnecessary, but forgive me. ・ I want to write java code ⇒ Enclose in <% ~%> ・ I want to output the value of a variable ⇒ Enclose it in <% = ~%> ・ I want to use the java library ⇒ Enclose in <% @ ~%> By the way, request and response can be used without declaring the library.

Continued to the third step

Recommended Posts

[Java Servlet] Senri no Michi is one step to the second
[Java Servlet] The road of Senri is also one step to the first
[Java Servlet] The road of Senri is also the fifth step from the first step
[Java Servlet] The road of Senri is also the third step from the first step
[Java Servlet] The road of Senri is also the fourth step from the first step
[Ruby] Hash # has_key? Is one step closer to the official deprecated?
What is the Java Servlet / JSP MVC model?
Java is the 5th day
Input to the Java console
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
How to increment the value of Map in one line in Java
[java8] To understand the Stream API
Where is the Java LocalDateTime.now () timezone?
Welcome to the Java Library Swamp! !!
The road from JavaScript to Java
[IOS] What to do when the image is filled with one color
Is Ruby's alias method the same no matter which one you use?
[Introduction to Java] I tried to summarize the knowledge that I think is essential
[Java small story] Monitor when a value is added to the List
From Java9, the constructor of the class corresponding to primitive types is deprecated.
What is CHECKSTYLE: OFF found in the Java source? Checkstyle to know from
[Java: memorandum] Until the line feed code CRLF is changed to LF