一个jsp+cgi+html小工程,完成注册,后台使用CGI
前提:安裝了APACHE 和 tomcat and jdk ,mysql
需求:完成一個客戶注冊的過程。
首先完成一個用戶注冊的test.html頁面該頁面調(diào)用一個test.jsp ,test.jsp獲取用戶輸入數(shù)據(jù)并再把它們傳給hello.cgi處理。最后將處理過的數(shù)據(jù)寫入數(shù)據(jù)庫。
準備工作:test.html(位于D:"local"htdoc)和hello.cgi(位于 D:"local"cgi-bin)是依靠于apache服務器的。需要配置下環(huán)境變量 打開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)。還有最前面的#為注釋,這樣我們在網(wǎng)頁中輸入http://localhost/test.html 即可看到注冊頁面。代碼不列了,就是個靜態(tài)html,注意提交的處理腳本就行了。如下
<FORM action="http://localhost:8080/test.jsp" method=post>?當提交之后就會調(diào)用tomcat根目錄下的test.jsp 全路徑為E:"Program Files"Tomcat 6.0"webapps"ROOT"test.jsp
接下來自然到了test.jsp文件了
?
內(nèi)容為:
<%@ 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表單用戶輸入的數(shù)據(jù)
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+"®_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;
//將用戶輸入的數(shù)據(jù)格式化,例如結(jié)果為:anth_type=1&user_name=熊健(片段),下面是關鍵了
?try {?
??????? URL u = new URL("http://localhost/cgi-bin/hello.cgi");
// 這個地址就是本jsp接下來要調(diào)用的程序為hello.cgi 位置在D:"local"cgi-bin
??????? URLConnection urlc = u.openConnection();
// urlc表示到 URL 的連接對象
??????? urlc.setDoOutput(true);
//指示應用程序要從 URL 連接讀取數(shù)據(jù)
??????? urlc.setDoInput(true);
??????? urlc.setAllowUserInteraction(false);
//不允許有任何用戶交互。
??????? DataOutputStream server =?new DataOutputStream(urlc.getOutputStream());
// urlc.getOutputStream()返回寫入到此連接的輸出流。Out是什么意思?就是把數(shù)據(jù)輸出到目的地。
//反過來說呢,就是把數(shù)據(jù)沖這里out(寫入)到URL中!
??????? server.writeBytes(totol);
??????? server.close();
??????? DataInputStream in = new DataInputStream(urlc.getInputStream());
//等待讀數(shù)據(jù)!
?????? 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();
//這一大段是讀數(shù)據(jù),看到了嗎,每次讀數(shù)據(jù)之間要各一個空行,我是試出來,不知道為什么。哈哈,太有才了
??????? 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+"')");
}
//數(shù)據(jù)庫操作,我不說了
catch(Exception e){out.print(e.toString());}
finally{out.print("reg success!");}
%>
?
注意,在server.writeBytes(totol);和in.readLine(); 之間是會等待hello.cgi執(zhí)行的。當hello.cgi執(zhí)行完畢之后返回結(jié)果本jsp才會繼續(xù)執(zhí)行。
?
下面看看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"); 這個輸出是把數(shù)據(jù)輸出到調(diào)用它的jsp,注意只是輸出給jsp但是網(wǎng)頁上還是沒有顯示,當jsp調(diào)用out.print時候才真正顯示。當然如果是html直接調(diào)用這個cgi那么這個輸出就是直接返回給Html了,簡單點說就是直接在網(wǎng)頁上顯示了。
2??????? 我們用的全部都是POST方法,那么調(diào)用getenv("CONTENT_LENGTH");就可以返回傳來的數(shù)據(jù)長度。cin.read(query_str, len);得到數(shù)據(jù)(只要是標準人輸入設備都可以得到,比如用getchar()或者其他文件讀函數(shù)得到所有的輸入;),存放在query_str 它的數(shù)據(jù)就是上面提到的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->數(shù)據(jù)庫操作.jsp->修改用戶數(shù)據(jù).cgi
其實還有管理員的功能的,但是比較簡單,這里就不列舉了~
轉(zhuǎn)載于:https://www.cnblogs.com/SuperXJ/archive/2009/09/27/1575252.html
總結(jié)
以上是生活随笔為你收集整理的一个jsp+cgi+html小工程,完成注册,后台使用CGI的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IIS 405 Method Not A
- 下一篇: Vue若依管理系统-实现管理员配置首页计