MVC Tutorial
-
Upload
ninh-luc-ton -
Category
Documents
-
view
213 -
download
0
Transcript of MVC Tutorial
![Page 1: MVC Tutorial](https://reader036.fdocuments.us/reader036/viewer/2022083114/577ccd171a28ab9e788b78f0/html5/thumbnails/1.jpg)
A Simple MVC-2 example
A simple example of MVC architecture
1.Open Netbeans 6.9 or 7.0
2.Click File->New Project->Java Web-> Web Application
We named it MVCServlet and choose the Tomcat as server.
3.Creating fromServlet.jsp, add the following code.<jsp:useBean id="beanInfo" class="myModel.MyBean" scope="session"/> <html>
set userName
display userName
JavaBean
userName
Create BeanStore userName in Bean
get userName
forward
myServlet.java
MyBean.Java
fromServlet.jsp
![Page 2: MVC Tutorial](https://reader036.fdocuments.us/reader036/viewer/2022083114/577ccd171a28ab9e788b78f0/html5/thumbnails/2.jpg)
<body> <b> Hello <jsp:getProperty name="beanInfo" property="userName"/></b></body></html> Or you can use JSP EL notation in the JSP file as follows. <html><body><b> Hello ${beanInfo.userName}!</b></html>
</body>
4.Creating MyservletRight click the project name and choose new->Servlet.
![Page 3: MVC Tutorial](https://reader036.fdocuments.us/reader036/viewer/2022083114/577ccd171a28ab9e788b78f0/html5/thumbnails/3.jpg)
Add the following code
package myControler;
import java.io.IOException;
![Page 4: MVC Tutorial](https://reader036.fdocuments.us/reader036/viewer/2022083114/577ccd171a28ab9e788b78f0/html5/thumbnails/4.jpg)
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;import myModel.MyBean;
@WebServlet(name = "MyServlet", urlPatterns = {"/MyServlet"})public class MyServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); MyBean myBean = new MyBean(); myBean.setUserName( "Kai"); HttpSession session = request.getSession(); session.setAttribute("beanInfo", myBean); RequestDispatcher rd; rd =getServletContext().getRequestDispatcher("/fromServlet.jsp"); rd.forward(request, response); }
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/** * Handles the HTTP <code>GET</code> method. * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); }
/** * Handles the HTTP <code>POST</code> method.
![Page 5: MVC Tutorial](https://reader036.fdocuments.us/reader036/viewer/2022083114/577ccd171a28ab9e788b78f0/html5/thumbnails/5.jpg)
* @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); }
/** * Returns a short description of the servlet. * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold>
}
5.Creating test pageNew a JSP file and add the following code.
<html><%@ page contentType="text/html;charset=gb2312"%> <head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>Name</title></head><body> <table border="0" width="100%"> <tr> <td><div align="center"><a href="/MVCServlet/MyServlet">Show name</a></div></td> </tr></table></body>
</html>6.Creating bean package and bean class.
Right click the project name, as the following graph.
![Page 6: MVC Tutorial](https://reader036.fdocuments.us/reader036/viewer/2022083114/577ccd171a28ab9e788b78f0/html5/thumbnails/6.jpg)
Add the following code into it.package myModel;
![Page 7: MVC Tutorial](https://reader036.fdocuments.us/reader036/viewer/2022083114/577ccd171a28ab9e788b78f0/html5/thumbnails/7.jpg)
public class MyBean { private String userName; public MyBean(){ userName = "";
} public void setUserName(String userName) { this.userName = userName; } public String getUserName() { return this.userName; }
}
7.Configure web.xml file.Click the Servlet tab as follows,
Click the Pages tab as follows,
![Page 8: MVC Tutorial](https://reader036.fdocuments.us/reader036/viewer/2022083114/577ccd171a28ab9e788b78f0/html5/thumbnails/8.jpg)
Finally, Click Run button to run the Project
Deployment Guide
![Page 9: MVC Tutorial](https://reader036.fdocuments.us/reader036/viewer/2022083114/577ccd171a28ab9e788b78f0/html5/thumbnails/9.jpg)
Right Click the Project Name and choose Clean and Build, it will generate .war file.
Then you will find .war file where is dist folder.
![Page 10: MVC Tutorial](https://reader036.fdocuments.us/reader036/viewer/2022083114/577ccd171a28ab9e788b78f0/html5/thumbnails/10.jpg)
Next, Copy the .war file into webapps folder of Tomcat.
Finally, Start the Tomcat server and type http://localhost:8088/MVCServlet/ into your browser, 8088 is my Port number. (Remember: after deploy, you may need to close Netbeans first then start Tomcat server )