使用Java / JSP重新排序表单结果

时间:2014-09-05 15:54:54

标签: java html jsp

我对Java和JSP缺乏经验。我创建了一个表单并且它按照预期的方式工作,但是我希望它有一些乐趣,并在表单提交后重新排序结果。我将包含一些图像来显示我的意思。我很难找到我想要的东西而且真的不知道从哪里开始。任何帮助将不胜感激。

以下是表单页面: enter image description here

结果如下:
enter image description here

这就是我想要的结果(注意'最后'从2到3,'中间'从3到5,'项'从4到2,'地址'从5到4): enter image description here

Java文件

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

public class ShowParameters extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String docType =
                "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
                "Transitional//EN\">\n";
        String title = "Reading All Request Parameters";

        out.println(docType +
                "<HTML>\n" +
                "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
                "<BODY BGCOLOR=\"#FDF5E6\">\n" +
                "<H1 ALIGN=CENTER>" + title + "</H1>\n" +
                "<TABLE BORDER=1 ALIGN=CENTER>\n" +
                "<TR BGCOLOR=\"#FFAD00\">\n" +
                "<TH>Parameter Name<TH>Parameter Value(s)");

        Enumeration paramNames = request.getParameterNames();
        while(paramNames.hasMoreElements()) {
            String paramName = (String)paramNames.nextElement();
            out.print("<TR><TD>" + paramName + "\n<TD>");
            String[] paramValues =
                    request.getParameterValues(paramName);
            if (paramValues.length == 1) {
                String paramValue = paramValues[0];
                if (paramValue.length() == 0)
                    out.println("<I>No Value</I>");
                else
                    out.println(paramValue);
            } else {
                out.println("<UL>");
                for(int i=0; i<paramValues.length; i++) {
                    out.println("<LI>" + paramValues[i]);
                }
                out.println("</UL>");
            }
        }

        out.println("</TABLE>\n</BODY></HTML>");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("STOP1\n");
        doGet(request, response);
    }

}

JSP文件

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Lab 3</title>
<style type="text/css">
.address {
height: 50px;
}
</style>
</head>
<body>

<body BGCOLOR="#FF0000">

<h1 align="center">Basic FORM</h1>
<form action="ShowParameters" method="post">
    First Name: <input type="text" name="first"> <br>
    Last Name: <input type="text" name="last" value="$"> <hr/>
    Middle Name: <input type="text" name="middle"> <br>
    Item: <input type="text" name="item"> <br>
    Address: <input type="text" name="address" class="address"> <br>
    Credit Card: <br> 
    <input type="radio" name="cardType" value="Visa">Visa <br>
    <input type="radio" name="cardType" value="MasterCard">MasterCard <br>
    Credit Card Number: <input type="text" name="cardNum"> <br><br>

    <center><input type="submit" value="Submit Order"></center>
</form>

</body>
</html>

2 个答案:

答案 0 :(得分:1)

不是通过request.getParameterNames得到参数的枚举,而是你可以按照你想要的顺序得到你期望的所有参数名的字符串数组,并且你可以循环遍历那个数组:

String[] paramNames = { "item", "last", "first" };
for(int i=0; i<paramNames.length; i++)
{
     out.print("<tr>");
     out.print("<td>" + paramNames[i] + "</td>");
     out.print("<td>");
     String[] paramValues = request.getParameterValues(paramNames[i]);
     ...
     ...
     out.print("</td>");
     out.print("</tr>");
}

请注意,您在代码中未执行的操作之一是使用</td></tr>的行正确关闭单元格。您还应该使用</li>关闭LI。

答案 1 :(得分:1)

而不是在servlet中创建html创建一个类来保存表单输入信息,如:

public class Person {

    private String firstName;
    private String midlleName;
    private String lastName;
    private String item;
    private String address;
    private String cardType;
    private String cardNumber;

    //getters and setters

}

在servlet中创建Person类的实例并设置值,然后只需添加person实例来请求并转发到jsp。

   Person person = new Person();
   person.setFirstName(request.getParameter("first"));
   //set other person values here
   request.setAttribute("person", person);
   request.getRequestDispatcher("filename.jsp").forward(request, response);

在jsp中显示如下:

<table border="2">
    <tr bgcolor="#FFAD00">
        <th>Parameter Name</th>
        <th>Parameter Value(s)</th>     
    </tr>
    <tr>
        <td>first</td><td>${person.firstName}</td>
    </tr>
    <tr>
        <td>item</td><td>${person.item}</td>
    </tr>
    <tr>
        <td>last</td><td>${person.midlleName}</td>
    </tr>   
    <tr>
        <td>address</td><td>${person.address}</td>
    </tr>
    <tr>
        <td>middle</td><td>${person.lastName}</td>
    </tr>
    <tr>
        <td>cardType</td><td>${person.cardType}</td>
    </tr>
    <tr>
        <td>cardNum</td><td>${person.cardNumber}</td>
    </tr>
</table>

优点:

  • 在html中轻松更改顺序。(只需移动<tr/>元素)
  • 无需循环。
  • 遵循面向对象编程(OOP)编程风格。