Load variables and data
To create and add a welcome page, we need to execute the following steps:
1. Create a WEB-INF/jsp/ directory structure under the src/main/webapp/ web; modify the content of the page home.jsp
<html>
<head>
<!-- Information about the page -->
<!--This is the comment tag-->
<title>Home</title>
</head>
<body>
<section>
<div class="jumbotron">
<div class="container">
<h1> ${model.fullName} </h1>
<p> Login Status ${login} </p>
</div>
</div>
</section>
</body>
</html>
We have two variables, one is model .fullName this will get from model and login status login is got from controller.
Create a model package and create a class UserModel
Add the variables userName and fullName.
package com.web1.model;
public class UserModel {
private String userName;
private String fullName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
}
On the controller class, edit file HomeController
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
UserModel userModel = new UserModel();
userModel.setFullName("Guest");
request.setAttribute("model", userModel);
String login ="Annonymous";
request.setAttribute("login", login);
RequestDispatcher rd = request.getRequestDispatcher("/views/web/home.jsp");
rd.forward(request, response);
}
We need to setAttribute login variable to display this variable on the view.