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 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
Create a new java file after creating a suitable package Enter HttpServlet in Superclass and click Browse Select HttpServlet from [Matching items] and click [OK] After creating the class, click [Source]-[Method Override] From Select method to override or implement Select "doGet", "doPost", "destroy", and "init (no arguments)" and click [OK]. 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
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
・ 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
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.
Recommended Posts