我想写个验证程序(JAVA),客户端输入用户名和密码,访问网络服务器的JSP程序,JSP进行验证,把验证结果反馈给客户端,现在问题是
1,JSP怎么样设定返回值,是用response么?
2,客户端怎么接受反馈回来的处理结果?
public String Httpcertify(){
String urlStr = "http://localhost:8080/examples/Certify.jsp";
urlStr = urlStr + "?userid=" + txtUserId.getText() + "&";
urlStr = urlStr + "passwd=" + txtPassWd.getText() + "&";
urlStr = urlStr + "macaddress=" + MacAddress; try{
URL url = new URL(urlStr);
HttpURLConnection hurlc =(HttpURLConnection)url.openConnection();
hurlc.setFollowRedirects(true);
hurlc.setInstanceFollowRedirects(true);
hurlc.setUseCaches(true);
hurlc.connect();
InputStream is=hurlc.getInputStream();
byte tmpbyte[]=new byte[is.available()];
int num=is.read(tmpbyte);
String abc = new String(tmpbyte);
String result = new String(tmpbyte);
return result;
}catch(IOException e){
return "99";
}
}
这段程序可以发出请求,jsp也可以正确去读参数和验证,现在就是不知道怎么传回反馈信息,求救大家,拜托!
1,JSP怎么样设定返回值,是用response么?
2,客户端怎么接受反馈回来的处理结果?
public String Httpcertify(){
String urlStr = "http://localhost:8080/examples/Certify.jsp";
urlStr = urlStr + "?userid=" + txtUserId.getText() + "&";
urlStr = urlStr + "passwd=" + txtPassWd.getText() + "&";
urlStr = urlStr + "macaddress=" + MacAddress; try{
URL url = new URL(urlStr);
HttpURLConnection hurlc =(HttpURLConnection)url.openConnection();
hurlc.setFollowRedirects(true);
hurlc.setInstanceFollowRedirects(true);
hurlc.setUseCaches(true);
hurlc.connect();
InputStream is=hurlc.getInputStream();
byte tmpbyte[]=new byte[is.available()];
int num=is.read(tmpbyte);
String abc = new String(tmpbyte);
String result = new String(tmpbyte);
return result;
}catch(IOException e){
return "99";
}
}
这段程序可以发出请求,jsp也可以正确去读参数和验证,现在就是不知道怎么传回反馈信息,求救大家,拜托!
[java applet]应该为Application的小应用程序,希望调用服务器端的程序
如果有问题,应该是 is.available(); 的问题。
除非是特殊的输入流,否则最好不要用这个方法。
JSP程序中返回值怎么设定?
用户不存在 返回10
密码错误 返回20
已经验证的用户 返回30
正确通过时 返回00
返回值,其实就是用 输出流,往里面写 字节。例如,out.write(30);
你是对的,是用输出流的。
但是我接受数据时
InputStream is=hurlc.getInputStream();
byte tmpbyte[]=new byte[is.available()];
int num=is.read(tmpbyte);
返回的数据在tmpbyte[]的位置固定么?
我现在测试时返回的数据格式为
13 10 13 10 13 10 30 13 10 13 10
这个位置固定么?
不好意思,我是刚学java,还不会解析xml。我把jsp的代码贴出来,你有时间帮我看看输出流中返回的数据位置是否固定(我现在测试是正常的)String strUserId=request.getParameter("userid");
String strPassWd = request.getParameter("passwd");
String strMacAddress = request.getParameter("macaddress");DocumentBuilderFactory objDbf=DocumentBuilderFactory.newInstance();
DocumentBuilder objDb=objDbf.newDocumentBuilder();
Document objDoc=objDb.parse(application.getRealPath("Certify.xml"));
Element objRoot=objDoc.getDocumentElement();
NodeList clnNod=objRoot.getChildNodes();for(int i=0;i<clnNod.getLength();i++){
Node objNod=clnNod.item(i);
NodeList clnNod2=objNod.getChildNodes();
Node objNod2=clnNod2.item(0);
if(objNod2.getFirstChild().getNodeValue().equals(strUserId)){
str_passwd =clnNod2.item(1).getFirstChild().getNodeValue();
str_macadd =clnNod2.item(2).getFirstChild().getNodeValue();
//密码确认
if(str_passwd.equals(strPassWd)){
//新規認証
if(str_macadd.equals(str_macflg)){
//MacAddress設定
clnNod2.item(2).getFirstChild().setNodeValue(strMacAddress);
//xml更新
TransformerFactory fac=TransformerFactory.newInstance();
Transformer tran=fac.newTransformer(); StreamResult result=new StreamResult(new FileOutputStream(application.getRealPath("Certify.xml")));
tran.transform(new DOMSource(objDoc.getDocumentElement()),result);
//新規認証成功
out.write(0);
}else {
//同一个机器的再認証
if(strMacAddress.equals(str_macadd)){
out.write(0);
//重複認証
}else{
out.write(30);
}
}
}else{
//密码错误
out.write(20);
}
break;
}else if (i == clnNod.getLength() - 1){
out.write(10);//`用户不存在
}
jsp 不固定,是因为有时候会输入页面上的空白字符。