把这些代码组成一个完整的java类发我
时间: 2024-10-22 14:05:42 浏览: 14
第一行代码Java源代码第11章课程代码JavaIO编
当然可以,下面是一个简单的Java Servlet示例,用于处理HTTP POST请求并展示表单参数。这个例子使用了JSP页面来呈现HTML表格,同时处理了无值、单值和多值的情况。请注意,这只是一个基础示例,实际项目中可能需要增加更多的异常处理和优化。
```java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class FormServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html><body>");
out.println("<table border='1'>");
out.println("<tr><th>参数名称</th><th>值</th></tr>");
Enumeration<String> paramNames = request.getParameterNames();
while (paramNames.hasMoreElements()) {
String paramName = paramNames.nextElement();
String[] paramValues = request.getParameterValues(paramName);
if (paramValues == null || paramValues.length == 0) {
out.println("<tr style='background-color: lightgray'><td>" + paramName + "</td><td>No Value</td></tr>");
} else if (paramValues.length == 1) {
out.println("<tr><td>" + paramName + "</td><td>" + paramValues[0] + "</td></tr>");
} else {
out.println("<tr><td>" + paramName + "</td><td><ul>");
for (String value : paramValues) {
out.println("<li>" + value + "</li>");
}
out.println("</ul></td></tr>");
}
}
out.println("</table>");
out.println("</body></html>");
} finally {
out.close();
}
}
}
```
要在web.xml文件中配置这个Servlet:
```xml
< servlet>
<servlet-name>FormServlet</servlet-name>
<servlet-class>com.example.FormServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FormServlet</servlet-name>
<url-pattern>/form</url-pattern>
</servlet-mapping>
```
然后访问`http://yourserver.com/form`来测试。
阅读全文