初探MVC

很好的东东哦。。。。。不错
原文地址:初探MVC 作者:泛音

   [转载]初探MVC

M部分是javabean中的User.java

V部分是failure.jsp;

       index.jsp;

       login.jsp;

       success.jsp

C部分是HelloServelt.java

       LoginProcess.java

 

V

V部分的login.jsp;

 

[转载]初探MVC

 <body>
    <form name="form1" method="post" action="LoginProcess"> 用户ID:
    <input type="text" name="username"><br>口  令&nbsp;:
       <input type="text" name="userpass"><br>
       <input type ="submit" value="登录">
       <input type ="reset" value="重置">
    </form>
  </body>

 

 

V部分的failure.jsp
  <body>
    failure.jsp
  </body>

 

V部分的 failure.jsp
  <body>
    failure.jsp
  </body>

 

 

M

 

package javabean;

public class User {
       private String username;
       private String userpass;
       public String getUsername()
       {
        return username;
       }
       public void setUsename(String username)
       {
        this.username=username;  
       }
       public String getUserpass()
       {
        return userpass;
       }
       public void setUserpass(String userpass)
       {
        this.userpass=userpass; 
       }
       public boolean check()
       {
         if(username == null || userpass == null)
         {
          return false;
         }
         if(username.equals("cdq")&&userpass.equals("123"))
         {
          return true;
         }
         else
         {
          return false;
         }
     }
}

[转载]初探MVC

C

servlet有两个

HelloServlet.java 和  LoginProcess.java

1

package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet {
    public HelloServlet() {
  super();
 }
    public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }
    public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.println("Test1 Servlet");

 }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
    doGet(request,response);
 }
    public void init() throws ServletException { 
 }
}

[转载]初探MVC

2

 

package servlet;
import java.io.IOException;
import javabean.User;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginProcess extends HttpServlet {
        public LoginProcess() {super();}
        public void destroy() {  super.destroy(); }
        public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
        response.setContentType("text/html");
        //第一步 获取用户的输入的信息
  String username = request.getParameter("username");
  String userpass = request.getParameter("userpass");
  //第二步 调用JavaBean
  User user = new User();
  user.setUsename(username);
  user.setUserpass(userpass);
  boolean b = user.check();
  //第三步 选择一个界面对用户进行响应
  String forward;
  if(b){
   forward = "success.jsp";}
  else{
   forward = "failure.jsp";}
  //将请求转发到forward这个页面
        RequestDispatcher rd = request.getRequestDispatcher(forward);
        rd.forward(request, response);}
        public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
                 doGet(request,response); }
        public void init() throws ServletException {}
}

[转载]初探MVC

 

你可能感兴趣的:(初探MVC)