|
 |
|
Java ผมขอคำแนะนำ เรื่องของ MVC ทีครับผมไม่ค่อยเข้าใจครับ |
|
 |
|
|
 |
 |
|
สวัสดีครับพี่ๆ ผมเป็นมือใหม่ พึ่งหัดอะครับ อ่านแล้วไม่ค่อยเข้าใจ mvc อะครับ
ผมขอ ยกตัวอย่าง code ทีผมหัดเขียนนะครับ
ส่วน model
connectDB.java
package model;
import java.sql.*;
public class connectDB {
public static Connection connectDB;
public static Connection getConnect(){
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
connectDB=DriverManager.getConnection("jdbc:mysql://localhost:3306/ajaxdb","root","");
System.out.print("success");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connectDB;
}
}
resumeDAO.java
public class resumeDAO {
private Connection connDB;
private String firstName;
public resumeDAO(){
connDB = connectDB.getConnect();
}
public String getResume(){
try {
Statement s = connDB.createStatement();
ResultSet r = s.executeQuery("select * from resume");
while(r!=null&&r.next()){
firstName = r.getString("firstName");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return firstName;
}
}
ส่วน control
simpleServlet.java
package control;
import java.io.IOException;
import javax.servlet.DispatcherType;
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 model.resumeDAO;
/**
* Servlet implementation class simpleServlet
*/
@WebServlet("/simpleServlet")
public class simpleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public simpleServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.print("hello javaservlet");
resumeDAO re = new resumeDAO();
request.setAttribute("first", re.getResume());
getServletContext().getRequestDispatcher("/index.jsp").forward(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
}
}
ส่วน view
index.jsp
<input type="text" value="${firstName}">
code ผม แค่ ดึง firstname จาก database และ ส่ง แสดงผลที่ index.jsp ครับ
คือ ผมงง อะครับ
1) getServletContext().getRequestDispatcher("/index.jsp").forward(request,response) นี้คือดึกข้อมูลจาก index.jsp ใช่ไหมครับ
2)
คือ ผมดูจากรูปแล้ว ผมเข้าใจระบบ mvc ว่า request-->servlet -->model-->database &&& database-->model-->servlet-->view(jsp)-->response แต่ทำไมผมต้องกด run ตรงไฟล์ javaservlet อะครับถึงจะแสดงข้อมูล คือ ผมกด run ตรงไฟล์ index.jsp ไม่มีข้อมูลใดๆแสดงขึ้นมาเลย แสดงว่า servlet ก็เป็นทั้ง request และ response เลยรึป่าวครับ คือผมเข้าใจแบบนี้อะครับ รบกวนช่วยแนะนำทางที่ถูกทีครับ ขอบคุณมากครับพี่ๆ
Tag : Java, JAVA

|
ประวัติการแก้ไข 2016-07-12 00:58:10 2016-07-12 01:00:12 2016-07-12 01:02:35 2016-07-12 01:03:37
|
 |
 |
 |
 |
Date :
2016-07-12 00:56:11 |
By :
Mythology |
View :
2694 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
|
|

|
Load balance : Server 05
|