Now that the basic test has been successful, we have perfected it and made it a more complete project.
Now we re-plan the logic.
Two pages
A login page
A welcome page
Login page Enter account password, login success, Jump landing Success Welcome page
Also, update user login information and log login log
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>标题</title>
<body>
${Info}
请输入用户名,密码登陆:
<form action="/bbs/checkLogin.do" meathod="post">
用户名:
<input type="text" name="userName">
<br>
密码:
<input type="password" name="password">
<br>
<input type="submit" value="登陆">
<input type="reset" value="重置">
</form>
</body>
If you use some C tags need <%@ taglib prefix= "C" uri= "Http://java.sun.com/jsp/jstl/core"%>
and to introduce the JSTL jar package
welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>标题</title>
<body>
登陆成功!
<br>
欢迎光临!
</body>
Controller
package com.bbs.web;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bbs.domain.User;
import com.bbs.service.UserService;
@Controller
public class LoginController {
@Autowired
private UserService userService;
@RequestMapping(value="/login")
public String toLoginPage(HttpServletRequest request){
return "login";
}
@RequestMapping(value="/checkLogin")
public String loginPage(HttpServletRequest request,User user){
Boolean loginSucess = userService.hasMatchUser(user);
if(loginSucess){
userService.loginSucess(userService.findUserByUserName(user));
request.getSession().setAttribute("Info","");
return "welcome";
}else{
request.getSession().setAttribute("Info","请重新输入!!");
return "redirect:/login.do";
}
}
}
Open login Display Sign-in page
Form submitted to Checklogin
Verify success, perform login successful operation
Failed Redirect to login page
Note here that the
Request.getsession (). SetAttribute ("Info", "Please re-enter!!");
is placed in the session, if only request.setattribute is not possible, the page cannot read
Because of the redirect.
Equivalent to re-a request, the value of the put in the change
By the way, here are some differences
Session once
Request requests once
Just like two people talking, request only when you say a word, there is one, others talk, or you said a word, changed
The conversation is you're going to go back and forth and say that he's all the same
For example, submitting a form is a request, opening a hyperlink is also a request, and when the request is over, it dies.
When the user first establishes a connection to the server, the server generates a session until the user leaves or times out, such as closing the browser session and destroying
Or, you're storing it in the request, and it's all there, if you redirect.
But, the session, how do you jump page, he is still in the
They can save a lot of things, can put things no difference, the difference is mainly in the declaration cycle
Instantiate an object directly in the Web contain.
Built-in objects
Type
Scope
PageContext
Javax.servlet.jsp.pageContext
Page
Request
Javax.servlet.http.HttpServletRequest
Request
Response
Javax.servlet.http.HttpServletResponse
Page
Session
Javax.servlet.http.HttpSession
Session
Application
Javax.servlet.ServletContext
Application
Config
Javax.servlet.ServletConfig
Page
Out
Java.servlet.jsp.JspWriter
Page
Page
Java.lang.Object
Page
exception
Java.lang.Throwable
Page
As the name implies, the built-in object is the original meaning, directly can use
Setting properties: public void SetAttribute (String name,object.value)
Get attribute: public void getattribute (String name)
As you can see from the table, their scope can be divided into four types
Within a page range: page
In the context of a single request to the server: request
In a single session range: Session
Within an application Server range: Application
The session object is mainly used to save the user's various information, until its life cycle exceeds or is artificially released, you can use the session object to determine whether this user is a legitimate user.
The session object is an instantiated object of the Javax.Servlet.http.HttpSession interface, and the session belongs to an object within the scope of the HTTP protocol, so only the unique HTTP packet has this interface, there is no inheritance relationship, the user is assigned a sess when connected to the server. Ion to the user.
SPRINGMVC Project Complete Example 08 front page and Knowledge Point summary