明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

[原創(chuàng)]過去不會(huì)用mvc,通過摸索終于明白了,自己寫了個(gè)例子,獻(xiàn)給那些迷茫的java愛好者!

[摘要]注:過去不會(huì)用mvc,經(jīng)過摸索終于明白了,自己寫了個(gè)例子,獻(xiàn)給那些迷茫的java愛好者!前提:我用的sql2k,database:clear,table:testbean(newid(int),n...
注:過去不會(huì)用mvc,經(jīng)過摸索終于明白了,自己寫了個(gè)例子,獻(xiàn)給那些迷茫的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">
 號(hào)碼:<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);
}
}

//目錄結(jié)構(gòu)
root
mail
 test.jsp
web-inf
 classes
bean
 FirstBean.class