网站首页/技术开发列表/内容

[原创]过去不会用mvc,通过摸索终于明白了,自己写了个例子,献给那些迷茫的java爱好者!!

技术开发2022-05-06阅读
注:过去不会用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

……

相关阅读