zoukankan      html  css  js  c++  java
  • 一个jsp+cgi+html小工程,完成注册,后台使用CGI


    前提:安装了APACHE 和 tomcat and jdk ,mysql

    需求:完成一个客户注册的过程。

    首先完成一个用户注册的test.html页面该页面调用一个test.jsp ,test.jsp获取用户输入数据并再把它们传给hello.cgi处理。最后将处理过的数据写入数据库。

    准备工作:test.html(位于D:"local"htdoc)和hello.cgi(位于 D:"local"cgi-bin)是依靠于apache服务器的。需要配置下环境变量 打开E:"Program Files"Apache2"conf"httpd.conf

    修改最下面的代码为

    <VirtualHost *:80>

    #    ServerAdmin webmaster@dummy-host.example.com

    #    DocumentRoot /www/docs/dummy-host.example.com

    #    ServerName dummy-host.example.com

             ScriptAlias /cgi-bin/ "d:/local/cgi-bin/"

             DocumentRoot d:/local/htdoc

    #    ErrorLog logs/dummy-host.example.com-error_log

    #    CustomLog logs/dummy-host.example.com-access_log common

    </VirtualHost>

    做了这些服务器就知道cgi 和 html 可以位于d:/local中了

    注意中间设置的路径即可(只有设置了才能在那个路径下使用cgi)。还有最前面的#为注释,这样我们在网页中输入http://localhost/test.html 即可看到注册页面。代码不列了,就是个静态html,注意提交的处理脚本就行了。如下

    <FORM action="http://localhost:8080/test.jsp" method=post> 当提交之后就会调用tomcat根目录下的test.jsp 全路径为E:"Program Files"Tomcat 6.0"webapps"ROOT"test.jsp

    接下来自然到了test.jsp文件了


    内容为:

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

    <%@ page language="java" %>

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

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

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

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

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

    <%@ page import="java.io.*" %>               //包定义,没什么说的

    <%     

    String auth_type = request.getParameter("auth_type");  

    String serial_no      =   request.getParameter("serial_no");

    String reg_code      =   request.getParameter("reg_code");

    String verification      =   request.getParameter("verification");

    String usr_name      =   request.getParameter("usr_name");

    String usr_company      =   request.getParameter("usr_company");

    String tel_no      =   request.getParameter("tel_no");

    String mail_addr      =   request.getParameter("mail_addr");

    String memo_info      =   request.getParameter("memo_info");

     //这一大段都是获取上面test.html表单用户输入的数据

    if("".equals(serial_no)||"".equals(reg_code)||"".equals(verification)||"".equals(usr_name)||"".equals(usr_company)||"".equals(tel_no)||"".equals(mail_addr))

                   {out.print("缺少输入");

                   return;

                   }

    //这里是判断输入是否为空,为空则返回,不能用 ==0 ==nu ==”” 这些切忌,我暂时也不知道为什么

    //"" 这就是一个String对象!!!

    String totol ="auth_type="+auth_type+"&serial_no="+serial_no+"&reg_code="+reg_code+"&verification="+verification+"&usr_name="+usr_name+"&usr_company="+usr_company+"&tel_no="+tel_no+"&mail_addr="+mail_addr+"&memo_info="+memo_info;

    //将用户输入的数据格式化,例如结果为:anth_type=1&user_name=熊健(片段),下面是关键了

     try { 

            URL u = new URL("http://localhost/cgi-bin/hello.cgi");

    // 这个地址就是本jsp接下来要调用的程序为hello.cgi 位置在D:"local"cgi-bin

            URLConnection urlc = u.openConnection();

    // urlc表示到 URL 的连接对象

            urlc.setDoOutput(true);

    //示应用程序要从 URL 连接读取数据

            urlc.setDoInput(true);

            urlc.setAllowUserInteraction(false);

    //不允许有任何用户交互。

            DataOutputStream server = new DataOutputStream(urlc.getOutputStream());

    // urlc.getOutputStream()返回写入到此连接的输出流。Out是什么意思?就是把数据输出到目的地。

    //反过来说呢,就是把数据冲这里out(写入)URL中!

            server.writeBytes(totol);

            server.close();

            DataInputStream in = new DataInputStream(urlc.getInputStream());

    //等待读数据!

           in.readLine(); auth_type = in.readLine();

                  in.readLine(); serial_no = in.readLine();

                   in.readLine();reg_code = in.readLine();

                   in.readLine();verification = in.readLine();

                   in.readLine();usr_name = in.readLine();

                   in.readLine();usr_company = in.readLine();

                   in.readLine();tel_no = in.readLine();

                   in.readLine();mail_addr = in.readLine();

                   in.readLine();memo_info = in.readLine();

    //这一大段是读数据,看到了吗,每次读数据之间要各一个空行,我是试出来,不知道为什么。哈哈,太有才了

            in.close();

         String driverName="com.mysql.jdbc.Driver";

         String userName="root";

         String userPasswd="cnk8";

         String dbName="test";

         String tableName="reglist";

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

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

         Connection connection=DriverManager.getConnection(url);

         Statement statement = connection.createStatement();

    statement.executeUpdate("insert into reglist(auth_type,serial_no,reg_code,verification,usr_name,usr_company,tel_no,mail_addr,memo_info) values('"+auth_type+"','"+serial_no+"','"+reg_code+"','"+verification+"','"+usr_name+"','"+usr_company+"','"+tel_no+"','"+mail_addr+"','"+memo_info+"')");

    }

    //数据库操作,我不说了

    catch(Exception e){out.print(e.toString());}

    finally{out.print("reg success!");}

    %>

    注意,在server.writeBytes(totol);和in.readLine(); 之间是会等待hello.cgi执行的。当hello.cgi执行完毕之后返回结果本jsp才会继续执行。

    下面看看hello.cgi

    首先打开VC新建一个控制台,写入下面代码

    #include "stdafx.h"

    #include <stdio.h>

    #include <stdlib.h>

    #include <iostream.h>

    #include "CGITools.h"

    int main()

    {

         printf("Content-Type: text/html"r"n"r"n");

         char* clen = getenv("CONTENT_LENGTH");

         if(clen == 0)

         {

             printf( "Zero CONTENT_LENGTH "n");

             return -1;

         }

         int len = atoi(clen);

         char* query_str = new char[len + 1];

         cin.read(query_str, len); //也可以用getchar  等

         query_str[len] = '"0';

         CGI_vector query(query_str);

         strcat(query[1].value(),"1");

         for(int i = 0; i < query.size(); i++)

         {   

             printf("%s"n",query[i].name());

             printf("%s"n",query[i].value()); //也可以用putchar cout 等

         }

         delete [] query_str;

         return 0;

    }

    这段代码特别注意!!

    1:printf("Content-Type: text/html"r"n"r"n"); 这个输出是把数据输出到调用它的jsp,注意只是输出给jsp但是网页上还是没有显示,当jsp调用out.print时候才真正显示。当然如果是html直接调用这个cgi那么这个输出就是直接返回给Html了,简单点说就是直接在网页上显示了。

    2        我们用的全部都是POST方法,那么调用getenv("CONTENT_LENGTH");就可以返回传来的数据长度。cin.read(query_str, len);得到数据(只要是标准人输入设备都可以得到,比如用getchar()或者其他文件读函数得到所有的输入;),存放在query_str 它的数据就是上面提到的anth_type=1&user_name=熊健

    3         CGI_vector 不知道是什么吧? 去看Think in Java(中文版).chm 其实这些代码很多都来自那里喔。还是说一下:vector<myclass> 看到了vector里面可以放类的。class CGI_vector : public vector< myclass >

    4        Vector本身就是类。CGI_vector是从他继承的。 这样我们在程序中

    CGI_vector query

    Query[0]; 这样就得到了第一个vector 中存放的myclass元素。

    OK注册页面和后台程序这样就完成了。注册.hml->数据库操作.jsp->修改用户数据.cgi


    其实还有管理员的功能的,但是比较简单,这里就不列举了~

  • 相关阅读:
    我不喜欢的 Rust 特性 (之一) eager drop
    为 Windows Phone 8.1 app 解决“The type does not support direct content.”的问题
    输入10个互不相同的数字并分成5对,问有多少种分法。
    code wars quiz: toInteger
    my first emacs custom key binding
    http协议消息报头学习笔记
    移动端经常遇到的小bug
    js小技巧
    ajax
    js正则表达
  • 原文地址:https://www.cnblogs.com/SuperXJ/p/1575252.html
Copyright © 2011-2022 走看看