A Simple MVC-2 example

tamerunΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 5 χρόνια και 1 μέρα)

319 εμφανίσεις

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, a
dd the following code.

<jsp:useBean id="beanInfo" class="myModel.MyBean" scope="session"/>


<html>


set
userName
display
userName
JavaBean
userName
Create Bean
Store 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 Myservlet

Right click the project name and choose new
-
>Servlet.




Add the following code



package myControler;


import java.io.IOException;


import javax.servlet.Reque
stDispatcher;

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 Serv
letException, IOException {


response.setContentType("text/html;charset=UTF
-
8");




MyBean myBean = new MyBean();


myBean.setUserName( "Kai");


HttpSession session = request.getSession();


session.setAttribute("beanI
nfo", 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


*/


@Ove
rride


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 page

New a JSP file and add the following code.

<html>

<%@ page conte
ntType="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;




pu
blic 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





Deploy
ment 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 clo
se
Netbeans first then start Tomcat server )