** ↑ Image of this framework **
This time is the mountain. I don't think it comes to my mind very much, but at first it is. Let's do our best.
Let's create a website using the framework of the subject Breakdown of all 4 times
--Eclipse installed --DB2 installed (required for 4th time)
Top.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%><!--Character code specification-->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Top</title>
<h1>top</h1>
</head>
<body>
<form action="/MVC/Controller" method="post"> <!--Declaration to send ↓ information in post [Addition] Set the action value to Controller-->
<input type="text" name="keyword"> <!--Search keyword input field-->
<input type="hidden" name="beanid" value="practice.Product"><!--[Add] Declare the bean to be used-->
<input type="hidden" name="pageto" value="Result.jsp"><!--[Addition] Specifying the transition destination-->
<input type="submit" value="Search"> <!--Keyword send button-->
</form>
</body>
</html>
Product.java
package practice;
public class Product extends SuperBean{
//Keep member variables private
private int price;
private int weight;
private String name;
private String material1;
private String category;
public void runBean(){//[Change] The method name has been changed from setAll.
String keyword = (String) super.request.getParameter("keyword");//[Change] Output the received value and check
System.out.println("The search keyword is"+keyword+"was. I will search the DB using this next time");
//Since we will not make a DB connection this time, we will set the value directly to each attribute of the bean.
this.price=1404;
this.weight=300;
this.name=""My disappearance" Fuminori Nakamura";
this.material1="paper";
this.category="book";
}
public int getPrice() {
return price;
}
public int getWeight() {
return weight;
}
public String getName() {
return name;
}
public String getMaterial1() {
return material1;
}
public String getCategory() {
return category;
}
}
Created to organize common bean processing into parent class
SuperBean.java
package practice;
import javax.servlet.http.HttpServletRequest;
public abstract class SuperBean {
protected HttpServletRequest request;//Hold request
void setJspInfo(HttpServletRequest request){
this.request = request;
}
protected void runBean(){//Run bean
}
}
Since then, Controller is designed so that editing is not required even when adding functions (jsp, beans). I think it's hard to write now, but it's useful when adding features. To create a Servlet, right-click the practice package → New → Servlet.
Controller.java
package practice;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class Controller
*/
@WebServlet("/Controller")
public class Controller extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public Controller() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//session(A container for returning an instance to the jsp side)Generate a.
HttpSession session= request.getSession();
//It takes the bean name as a variable and instantiates that bean. This allows you to use methods such as getters. The Product class is instantiated here.
//Character code specification
request.setCharacterEncoding("UTF-8");
String beanid = (String)request.getParameter("beanid");
String pageto = (String)request.getParameter("pageto");
try {
SuperBean bean = (SuperBean) Class.forName(beanid).newInstance();
//Pass the information received from jsp to the bean
bean.setJspInfo(request);
//Run bean
bean.runBean();
//Store beans in session
session.setAttribute("bean", bean);
//transition
RequestDispatcher rd =request.getRequestDispatcher(pageto);
rd.forward(request, response);
} catch (InstantiationException e) {
//TODO auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
//TODO auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
//TODO auto-generated catch block
e.printStackTrace();
}
}
}
Result.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="practice.Product"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%request.setCharacterEncoding("UTF-8"); %>
<%Product bean = (Product)session.getAttribute("bean"); %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Product name:<%=bean.getName() %><br>
price:\ <%=bean.getPrice() %><br>
weight:<%=bean.getWeight() %><br>
Product Category:<%=bean.getCategory() %><br>
</body>
</html>
This completes MVC excluding DB connection. The content of this time was much more difficult than before. Thank you for your hard work. I think I'll run into errors, so I'm waiting for your question.
Recommended Posts