日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

一个jsp+cgi+html小工程,完成注册,后台使用CGI

發布時間:2023/12/16 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一个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


其實還有管理員的功能的,但是比較簡單,這里就不列舉了~

轉載于:https://www.cnblogs.com/SuperXJ/archive/2009/09/27/1575252.html

總結

以上是生活随笔為你收集整理的一个jsp+cgi+html小工程,完成注册,后台使用CGI的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。