• 开始 jsp useBean 使用


     

    流程为:浏览器输入,调用1.jsp 1.jsp调用proBean.class的方法完成插入数据库操作

       useBean 就相当于c++的类!

    写一个proBean.java 内容为:

    package packet;

    import java.io.*;

    import javax.servlet.*;

    import javax.servlet.jsp.*;

    import java.sql.*;

    public class proBean

    {

                  //定义内部使用的相应对象变量

                                             String ID ;

                                              String gridno ;

                                              String type ;

                                              String x ;

                                              String y ;

                                              String name ;

                                              String dbName;

                                              String tableName;

                                              String m_URL;

                                              String userName;

                                              String userPasswd;

                                              Connection Con;

                                        Statement Stat;

           ServletRequest req;

           ServletResponse   res;

           ServletOutputStream out;

                 

                  public proBean()     {}

          

                  public void init(ServletRequest request,ServletResponse response)

           {

                                      

                  /*try

                         {

                                out=res.getOutputStream();

                         }

                   catch(IOException e)

                         {

                                System.out.println("getOutputStream Error:"+e.getMessage());

                         }

                         */

                                       try

                                       {

                                                     req=request;

                                          res=response;   

                                               dbName="test";   //数据库名字

                                               tableName="point"; //

                                               userName="root";

                                                     userPasswd="cnk8";

                                                     m_URL="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;

                                                     Class.forName("com.mysql.jdbc.Driver").newInstance();

                                                     Con=DriverManager.getConnection(m_URL);

                                                     Stat = Con.createStatement();

                                           ID      = req.getParameter("ID");

                                                     gridno = req.getParameter("gridno");

                                                     type    = req.getParameter("type");

                                                     x       = req.getParameter("x");

                                                     y       = req.getParameter("y");

                                                     name    = req.getParameter("name");                

                                       }

                                                            catch(Exception e)

                                                                   {

                                                                   System.out.println("getOutputStream Error:"+e.getMessage());

                                                                   }

     

           }

          

           public void execute()

           {

                  try{

                                       Stat.executeUpdate("insert into point(ID,gridno,type,x,y,name) values('"+ID+"','"+gridno+"','"+type+"','"+x+"','"+y+"','"+name+"')");

                          }

                         catch(SQLException e)

                  {

                         System.out.println("getOutputStream Error:"+e.getMessage());

                  }

           }

                        

           public void clean()

           {

                         req=null;

                         res=null;

                         try{

                  //     rs.close();

                         Stat.close();

                         Con.close();

                  }

                  catch(SQLException e)

                  {System.out.println("getOutputStream Error:"+e.getMessage());}

                 

           }

    }

    好了现在把这个源文件编译一下用: javac proBean.java

    就可以得到proBean.class 这个二进制执行文件了。(jsp文件调用的是这个.class

    注意:修改了web.xml以及新加了class,都要重启Tomcat

     

    程序不用看了!其实就相当于C++的一个类,有三个方法init(),execute(),clean()

     。注意几点就行:

    1package packet; 这是设置一个package 如果不加会错!proBean.java 放到

    .. "Tomcat 6.0"webapps"ROOT"WEB-INF"classes"packet 里面!"packet该目录就是包名字。

    2:用cmd到该目录 运行 javac proBean.java 会生成proBean.class 这就是等会1.jsp要用的类了!当然先要加入环境变量,否则编译不通过,因为找不到import javax.servlet.*;

    import javax.servlet.jsp.*;

    import java.sql.*;

    等库文件.

    环境变量这样设置:

     

    CLASSPATH

    .;   //表示当前目录

    E:"Program Files"Tomcat 6.0"lib"servlet-api.jar;

    E:"Program Files"Tomcat 6.0"lib"jsp-api.jar;

    E:"Program Files"Tomcat 6.0"lib;

    E:"Program Files"Java"jdk1.6.0_12"lib"dt.jar;

    E:"Program Files"Java"jdk1.6.0_12"lib"tools.jar;

    E:"Program Files"Java"jdk1.6.0_12"jre"lib"rt.jar

     

    PATH

    E:"Program Files"Java"jdk1.6.0_12"bin;

    E:"Program Files"Apache2"mysql"bin

    %SystemRoot%"system32;

    %SystemRoot%;

    %SystemRoot%"System32"Wbem;

    C:"Program Files"Microsoft SQL Server"90"Tools"binn";

    C:"Program Files"TortoiseSVN"bin;

     

    JAVA_HOME

    E:"Program Files"Java"jdk1.6.0_12

     

    TOMCAT_HOME

    E:"Program Files"Tomcat 6.0

     

    3嗯。。?,还要说一下在linux里面的配置方法其他都一样什么mysql-connector.jar放到tomcat/lib里面啊等关键就是把环境变量加到 /etc/profile 里面具体如下

    关闭防火墙 service iptables stop 开启tomcat ./startup.sh 没什么其他问题了go

     

    再写一个1.jsp ,内容为

    <%@include file ="head.jsp" %>

    <jsp:useBean id="inserpro" scope="page" class="packet.proBean" />

    <%

    try

    {

           inserpro.init(request,response);

        inserpro.execute();

            inserpro.clean();            

    }

    catch(Exception ex)

                  {

                  response.sendError(500,"sorry,some wrong happend");

                         out.println(ex.getMessage());

                  }

    %>

    <jsp:forward page = "done.jsp">

    <jsp:param name="p" value = "ok"/>

    </jsp:forward>

    注意3点:

    1<jsp:useBean id="inserpro" scope="page" class="packet.proBean" />

    Packet就是上面proBean.java 里设置的packet包。

    2%@include file ="head.jsp" %  head.jsp 1.jsp都在"Tomcat 6.0"webapps"ROOT这个目录下。 Head.jsp 就是个头文件里面是初始化,具体内容为:

    <%@ page contentType="text/html; charset=gb2312" %>

    <%@ page language="java" %>

    <%@ page import="com.mysql.jdbc.Driver" %>

    <%@ page import="java.sql.*" %>

    <%@ page import="java.util.Enumeration"%>

    <%@ page import="java.util.*" %>

    <%@ page errorPage="error.jsp" %>

    <%@ page session="true" %>

    <%!       String driverName="com.mysql.jdbc.Driver";%>

    内容也很简单没什么说的就是注意<%@ page errorPage="error.jsp" %>

    错误了就自动跳转到error.jsp

    3:最后的jsp:forward 表示运行到这里就跳转到done.jsp(这里面就是个输出!OK 并且传一个参数p=ok过去

     

     

     

    注意文件组织结构图:

    这些在ROOT下面。

    OK了,就这些文件就可以了!在网址处输入http://localhost:8080/jspTEST.jsp?ID=10112301&gridno=1001&type=200&x=1.1&y=1.1&name=text2 就会显示OK 数据库里就插入一条数据了。

    socket调用也是一样,前面有,俺不说了。

  • 相关阅读:
    JavaScript var,let,const三个关键字的区别
    nuxt head 配置
    阿里云MySQL安装到centos,并链接。
    js Array 的所有方法
    关于手机某些字体显是不全
    关于设备与canvas画不出来的解决办法
    关于github 新工程上传代码 git 命令
    高德地图3D菱形 区域点击搜索
    高德地图行政区域划分(西安)
    vue2获取dom节点
  • 原文地址:https://www.cnblogs.com/SuperXJ/p/1575246.html
Copyright © 2020-2023  润新知