English 中文(简体)
为什么我的CGI Java对 HTML 输出最终会被切断?
原标题:Why does my CGI Java to HTML output get cut off at the end?
  • 时间:2012-05-24 21:42:14
  •  标签:
  • java
  • html
  • cgi

我试图将 Java 输出为 HTML, 使用 CGI 脚本作为班级的 CGI 脚本 。

我想知道为什么我似乎无法正确比较我所检查的变量的值。 代码有效, 但无法比较变量, 也无法正确结束 HTML 代码。 出于某种原因, 它一直无法到达代码的结尾 。 ( 从来没有写出结束体和 html 标记 )

2001年11月18日至23日,日内瓦

import java.io.*;

public class post {
    public static void main(String [] args) {       
        System.out.println("Content-Type: text/html

");
        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

The HTML file that I m using to allow input: 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>

如果您可以指出任何问题... 调试输出有效, 因为它可以输出变量值, 但是完全忽略了由于某种原因的语句 。

最佳回答

我想知道为什么我似乎无法适当地比较 我所检查的变量的值。

因为您要对字符串 引用 来表示平等, 而不是字符串 的 内容。 每次您使用类似 :

if (city != "")

您想要:

if (!city.equals(""))

或者,如果 city 可能是无效的:

if (!"".equals(city))

!\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\引用的引用的引用的引用 \ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...