欢迎光临
我们一直在努力

[原创]过去不会用mvc,经过摸索终于明白了,自己写了个例子,献给那些迷茫的java爱好者!!-JSP教程,资料/其它

建站超值云服务器,限时71元/月

注:过去不会用mvc,经过摸索终于明白了,自己写了个例子,献给那些迷茫的java爱好者!!
前提:
我用的sql2k,database:clear,table:testbean(newid(int),name(char),addr(char))
容器用的tomcat
//
test.jsp:
<%@ page contenttype="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<body>
<center>
<%=(string)request.getattribute("alert")!=null?(string)request.getattribute("alert"):""
    %>
<p>
<p>
<form name=f method="post" action="/servlet/firstbean?req=no">
   号码:<input type=text name=deleteno size=4>
   <br>
   <input type=submit>
</form>
</center>
<body>

//bean:checkno
package bean;
import java.sql.*;
public class checkno
{
  private int no;
  int val;
  //
  public void setno(int no){
      this.no=no;
  }
  //
  public int getno(){
      return no;
  }
  //
  public int execdelete() throws classnotfoundexception,sqlexception{
      string con="delete from beantest where newid="+no;
      class.forname("com.microsoft.jdbc.sqlserver.sqlserverdriver");
      string url2="jdbc:microsoft:sqlserver://192.168.0.3:1433;databasename=clear";
      string user="sa";
      string password="pft";
      connection conn= drivermanager.getconnection(url2,user,password);
      statement st=conn.createstatement();
      val=st.executeupdate(con);
      st.close();
      conn.close();
      return val;
  }
}

//servlet:firstbean
  import javax.servlet.*;
  import javax.servlet.http.*;
  import java.sql.*;
  import java.io.*;
  import bean.*;
  public class firstbean extends httpservlet {
     //定义全局变量
     int theno;
     //init
     public void init() throws servletexception {
         super.init();
     }
     //doget
     public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
        string req = request.getparameter("req");
        if(req==null){redirect(request,response,"test.jsp");}
     }
     //dopost
     public void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
        try{
         string req = request.getparameter("req");
        if(req==null){redirect(request,response,"test.jsp");}
        else if(req.equals("no")){
          theno=integer.parseint(request.getparameter("deleteno"));
          begin(request,response);
        }
        }catch(exception e){
            system.out.print(e.tostring());
        }
     }
     //begin
     public void begin(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception{
       try{
       checkno cn=new checkno();
       cn.setno(theno);
       int val=cn.execdelete();
       if(val==1){
           //system.out.print("successful!");
           request.setattribute("alert","删除成功");
           redirect(request,response,"test.jsp");
       }
       else{
           //system.out.print("failed!");
           request.setattribute("alert","删除失败");
           redirect(request,response,"test.jsp");
       }
       }catch(exception e){
        system.out.print(e.tostring());
       }
     }
     //redirect
     public void redirect(httpservletrequest request, httpservletresponse response, string directory) throws servletexception, ioexception {
        requestdispatcher redirectory = getservletcontext().getrequestdispatcher("/mail/" + directory);
        redirectory.forward(request,response);  
        }
  }

//目录结构
root
    mail
       test.jsp
    web-inf
       classes
          bean
             firstbean.class
        

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » [原创]过去不会用mvc,经过摸索终于明白了,自己写了个例子,献给那些迷茫的java爱好者!!-JSP教程,资料/其它
分享到: 更多 (0)