How servlet the variables displayed in the jsp page - object-oriented approach

// a step of servlet
// 1 acquires parameters in the address bar
String = request.getParameter username ( "username");
String password = request.getParameter ( "password");

//2.创建对象
User user = new User(username, password, realname, gender, age);

// 3. Analyzing, jump
IF ( "zhangsan" .equals (username) && "123456" .equals (password)) {
// a pair of key-value in the request scope
request.setAttribute ( "user" , User);
// then forwards the request to another page (the index.jsp) to get this value (value)
request.getRequestDispatcher ( "the index.jsp") forward (request, Response);.
}
}

// two in jsp settings
<! - here with a strong turn, will be strong into the Object class User class ->
Welcome <% = ((User) ( request.getAttribute ( "user"))) getUsername. () come back!
real name: <% = ((User) request.getAttribute ( "user")) getRealname ()%.>
gender: <% = ((User) request.getAttribute ( "user")) getGender (. )%>
Age:. <% = ((User ) request.getAttribute ( "user")) getAge ()%>

Disadvantages: in the jsp writing complicated, and can not directly access the jsp, otherwise it will error, so to judge, write a lot of code

Guess you like

Origin www.cnblogs.com/su-chu-zhi-151/p/11221838.html