我在一个带有几个输入参数的servlet中有一个HTML表单,当我按下Submit按钮时,我想从表单中获取值并将其写入文件。我不确定如何捕获在下一个.jsp中使用的parms。这是我的代码:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.text.*;
import java.util.Random;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class CreateCust extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
// Generate a Customer Account using the Random Number Generator
// Calculate Confirmation Number
Random randomGenerator = new Random();
int custNum = randomGenerator.nextInt(10000);
String custId = "CST" + custNum;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Customer Profile Information";
String MainPageURL =
response.encodeURL("/csj/CustomerAccounts.html");
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType +
"<HTML>\n" +
"<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1 ALIGN=\"CENTER\">" + title + "</H1>");
out.println("<BR>" +
"<BR>" +
"<a href=" + MainPageURL + ">Previous Page</a>");
out.println
("<Form>\n" +
"<CENTER>" +
"Please Enter the following Information:\n" +
"<TABLE border=1>" +
"<TR>" +
"<TD>" +
"Customer ID: " +
"</TD>" +
"<TD>" +
"<Label>" + custId + " </Label>\n" +
"</TD>" +
"</TR>" +
"<TR>" +
"<TD>" +
"Name:" +
"</TD>" +
"<TD>" +
"<Input type=\"Text\" name=\"F_name\" </input>\n" +
"</TR>" +
"<BR>" +
"<TR>" +
"<TD>" +
"Street Address:" +
"</TD>" +
"<TD>" +
"<Input type=\"Text\" name=\"F_Add1\" </input>\n" +
"</TD>" +
"</TR>" +
"<BR>" +
"<TR>" +
"<TD>" +
"Apt or Suite:" +
"</TD>" +
"<TD>" +
"<Input type=\"Text\" name=\"F_Add2\" </input>\n" +
"</TD>" +
"</TR>" +
"<BR>" +
"<TR>" +
"<TD>" +
"City:" +
"</TD>" +
"<TD>" +
"<Input type=\"Text\" name=\"F_City\" </input>\n" +
"</TD>" +
"</TR>" +
"<BR>" +
"<TR><TD>" +
"State:" +
"</TD><TD>" +
"<Input type=\"Text\" name=\"F_state\" </input>\n" +
"</TD>" +
"</TR>" +
"<BR>" +
"<TR><TD>" +
"Zip Code:" +
"</TD>" +
"<TD>" +
"<Input type=\"Text\" name=\"F_Zip\" </input>\n" +
"</TD>" +
"</TR>" +
"<BR>" +
"<TR>" +
"<TD>" +
"Phone Number:" +
"</TD>" +
"<TD>" +
"<Input type=\"Text\" name=\"F_Phone\" </input>\n" +
"</TD>" +
"</TR>" +
"<BR>" +
"<TR><TD>" +
"Cable Plan:" +
"</TD>" +
"<TD>" +
"<select>\n" +
"<option value=\"selcab\">Select Cable</option>\n" +
"<option value=\"Basic50\">Basic50</option>\n" +
"<option value=\"Basic100\">Basic100</option>\n" +
"<option value=\"Ultimate200\">Ultimate200</option>\n" +
"</select>\n" +
"</TD>" +
"</TR>" +
"<BR>" +
"<TR><TD>" +
"Internet Plan:" +
"</TD>" +
"<TD>" +
"<select>\n" +
"<option value=\"selInt\">Select Internet</option>\n" +
"<option value=\"speedlane\">SpeedLane</option>\n" +
"<option value=\"lightlane\">LightLane</option>\n" +
"</select>\n" +
"</TD>" +
"</TR>" +
"<TABLE>" +
"</CENTER>" +
"</Form>" );
String confirmURL =
response.encodeURL("/csj/ConfirmCust");
// "Proceed to Checkout" button below table
out.println
("</TABLE>\n" +
"<FORM ACTION=\"" + confirmURL + "\">\n" +
"<BIG><CENTER>\n" +
"<INPUT TYPE=\"SUBMIT\"\n" +
" VALUE=\"Create Account\">\n" +
"</CENTER></BIG>" +
"</FORM>");
out.println("</BODY></HTML>");
}
}
答案 0 :(得分:2)
当我按下提交按钮时,我想从表单中获取值
为此,你必须request.getParameter("input parameter name");
在你的servlet中你有两个表单,一个有动作而另一个没有。
见这个
out.println
("</TABLE>\n" +
"<FORM ACTION=\"" + confirmURL + "\">\n" +
"<BIG><CENTER>\n" +
"<INPUT TYPE=\"SUBMIT\"\n" +
" VALUE=\"Create Account\">\n" +
"</CENTER></BIG>" +
"</FORM>");
此处存在表单操作,但是当您按下提交时,它会调用confirmURL
但会传递mo值(因为它没有任何输入参数)。
现在在此
out.println
("<Form>\n" +
"<CENTER>" +
"Please Enter the following Information:\n" +
"<TABLE border=1>" +
"<TR>" +
"<TD>" +
"Customer ID: " +
"</TD>" +
"<TD>" +
"<Label>" + custId + " </Label>\n" +
....................
输入参数存在但没有提交按钮和表单操作 因此不会执行任何操作,因此不会传递任何值。