mvc tutorial
TRANSCRIPT
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
<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.
Add the following code
package myControler;
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;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.
* @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.
Add the following code into it.package myModel;
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,
Finally, Click Run button to run the Project
Deployment Guide
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.
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 )