Now the project looks like -
PersonModel.java -
package com.mqin.example;
public class PersonModel {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Update service code in ServletController -
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
String firstName = request.getParameter("firstname");
String lastName = request.getParameter("lastname");
if (firstName == null
|| lastName == null
|| firstName == ""
|| lastName == "") {
getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
return;
}
PersonModel person = new PersonModel();
person.setFirstName(firstName);
person.setLastName(lastName);
request.setAttribute("person", person);
getServletContext().getRequestDispatcher("/output.jsp").forward(request, response);
}
Also update output page with <jsp:useBean> and <jsp:getProperty> tag -
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<jsp:useBean id="person" scope="request" class="com.mqin.example.PersonModel" />
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
<h1>Your first and last name is:</h1>
<table>
<tr>
<td>First Name:</td>
<td><jsp:getProperty name="person" property="firstName" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><jsp:getProperty name="person" property="lastName" /></td>
</tr>
</table>
</body>
</html>
Now we have finished the simplest Java MVC web application.
No comments:
Post a Comment