2012-05-24 49 views
1

我想使用類的CGI腳本輸出Java到HTML。爲什麼我的CGI Java to HTML輸出在最後被切斷?

我想知道爲什麼我似乎無法正確比較我檢查的變量的值。該代碼有效,但無法設法比較這些變量,並且不能正確結束HTML代碼。由於某種原因,它從未達到代碼的末尾。 (從來沒有寫出結束的身體和HTML標記)

post.java

import java.io.*; 

public class post { 
    public static void main(String [] args) {  
     System.out.println("Content-Type: text/html\n\n"); 
     System.out.println("<HTML><BODY>"); 
     System.out.println("<h1>Here is your stupid user report:</h1>"); 
     String fullname = ""; 
     String address = ""; 
     String city = ""; 
     String state = ""; 
     String washhands = ""; 
     String takeshower = ""; 
     boolean stupidity = false; 
     String comments = "";  
     String data = ""; 
     try { 
      BufferedReader br = 
       new BufferedReader(new InputStreamReader(System.in)); 
      data = br.readLine(); 
     } catch(IOException ioe) { 
      System.out.println ("IOException reading POST data: " + ioe); 
     }    
     if (data.length() > 0) { 
      String[] paramArray = data.split("&");  
      for(String param : paramArray){ 
       String[] paramVals = param.split("="); 
       String paramName = paramVals[0]; 
       System.out.println(paramName + "<br/>"); 
       String paramVal = paramVals[1]; 
       System.out.println(paramVal + "<br/>"); 
       if (paramName == "fullname") fullname = paramVal; 
       if (paramName == "address") address = paramVal; 
       if (paramName == "city") city = paramVal; 
       if (paramName == "state") state = paramVal; 
       if (paramName == "washhands") washhands = paramVal; 
       if (paramName == "takeshower") takeshower = paramVal; 
       if (paramName == "stupidity") stupidity = true; 
       if (paramName == "comments") comments = paramVal; 
      } 
      System.out.println("HELLOOOOO"); 
      if (fullname != ""){ 
       System.out.println("Here we have <font color=blue>" + fullname + "</font> "); 
      } 
      if (fullname == ""){ 
       System.out.println("Here's a <i>nobody</i> "); 
      } 
      if (address != ""){ 
       System.out.print("who lives on <font size=20>" + address + "</font> "); 
      } 
      if (city != ""){ 
       System.out.print("from the city of <u>" + city + "</u> "); 
      } 
      if (state != ""){ 
       System.out.print("in the state of <b><u>" + state + "</u></b> "); 
      } 
      if (washhands != ""){ 
       if (washhands == "no") System.out.print("who <b>doesn't</b> wash his/her hands after using the bathroom..."); 
       if (washhands == "yes") System.out.print("who washes his/her hands after using the bathroom..."); 
      } 
      if (takeshower != ""){ 
       if (takeshower == "everyday") System.out.println("<center>He/she takes a shower everyday.</center>"); 
       if (takeshower == "never") System.out.println("<center>He/she never takes a shower.</center>"); 
      } 
      if (stupidity == true){ 
       System.out.println("<tt>He/she is stupid.</tt>"); 
      } 
      if (comments != ""){ 
       System.out.println("<h3>Here are what he thinks about this assignment:" + comments + "</h3>"); 
      } 
     }   

     System.out.println("</BODY></HTML>"); 
    } 
} 

,我使用的CGI文件 - post.cgi(位於/cgi-bin/post.cgi)

#!/bin/sh 

java post 

,我使用允許輸入HTML文件: post.html

<html> 
<head> 
<title>Lab 9 Form</title> 
</head> 
<body> 
<h1>Embarrassing questions form</h1> 
<form action="../cgi-bin/post.cgi" method="post" name="embarassmentform"> 
<label for="fullname">Name</label> 
<input type="text" name="fullname" id="fullname" /> 
<br/><label for="address">Address</label> 
<input type="text" name="address" id="address" /> 
<br/><label for="city">City</label> 
<input type="text" name="city" id="city" /> 
<br/><label for="selectastate">State</label><select name="selectastate"> 
<option>NY</option> 
<option>FL</option> 
</select> 
<p/> 
<label for="washhands">Do you wash your hands after using the bathroom?</label> 
<br/><input type="radio" name="wash" value="yes">Yes</input> 
<br/><input type="radio" name="wash" value="no">No</input> 
<p/> 
<label for="takeshower">How often do you take a shower?</label> 
<br/><input type="radio" name="shower" value="everyday">Every day</input> 
<br/><input type="radio" name="shower" value="never">Never</input> 
<p/> 
<input type="checkbox" name="stupidity" value="stupid"/><label for="stupidity">Check this box if you're stupid.</label> 
<p/> 
<label for="comments">Questions and comments can go here</label><br/><textarea name="comments"></textarea> 
<br/><input type="submit" name="submitButton" value="Submit the form" /> 

</form> 

</body></html> 

如果你能認罪e指出任何可能出現問題的東西......調試輸出的工作原理,因爲它會管理輸出變量值,但由於某種原因完全忽略了if語句。

+0

Stackoverflow不會讀通過所有。請嘗試將其縮小到更具體的問題。換句話說:調試。 – OmniOwl

+0

問題是爲什麼它忽略了if語句... –

+0

然後你應該把它縮減到這個範圍,在你展示你調試它之後。 – OmniOwl

回答

2

我想知道爲什麼我似乎無法正確比較我檢查的變量的值。

因爲你比較字符串引用平等,而不是字符串的內容。每次你使用類似時間:

if (city != "") 

你想:

if (!city.equals("")) 

,或者如果city可以爲null:

if (!"".equals(city)) 

應用==!=以引用總是只是比較Java中的引用 - 沒有運算符重載以允許比較對象的內容。

+0

這就是答案。謝謝。 –