我试图在servlet中创建一个注册页面...使用eclipse的tomcat 7,它返回空白页面,我已经尝试了可用的步骤但是没有去...请帮助....
这是JSP页面
<form method="post" action="mbregistrationservlet">
First Name: <input type=text name=firstfame><br>
Last Name: <input type=text name=lastname></br>
Gender: <input type="radio" name="gender" Value="Male" checked>Male
<input type="radio" name="Gender" Value="Female">Female</br>
E-mail: <input type="email" name="email"><br>
Password:<input type="password" name="password"><br>
Security Question<select name="securityquestion">
<option value="Name of your first pet">Name of your first pet</option>
<option value="Name of your first byke">Name of your first byke</option>
<option value="Name of your first car">Name of your first car</option>
<option value="Name of your first school">Name of your first school</option>
<option value="Name of your first GF/BF">Name of your first GF/BF</option>
</select><br>
Answer: <input type="text" name="answer"><br>
Telephone:<input type="text" name="telephone"><br>
Address:<input type="text" name="address"><br>
City:<input type="text" name="city"><br>
State:<input type="text" name="state"><br>
Country:<input type="text" name="country"><br>
<input type="submit" value="suuubmit" name="Submit">
</form>
Servlet页面
public class mbregistrationservlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public static boolean register(String firstname, String lastname, String gender,String email, String password, String securityquestion, String answer, String telephone, String address, String city, String state, String country) throws IOException
{
HttpServletResponse response = null;
//response.setContentType("text/html");
PrintWriter out = response.getWriter();
boolean x =false;
try{
/*dbconn obj=new dbconn();*/
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","******","*******");
PreparedStatement ps = con.prepareStatement("insert into mbregistration values(?,?,?,?,?,?,?,?,?,?,?,?)");
ps.setString(1,firstname);
ps.setString(2,lastname);
ps.setString(3,gender);
ps.setString(4, email);
ps.setString(5, password);
ps.setString(6, securityquestion);
ps.setString(7, answer);
ps.setString(8, telephone);
ps.setString(9, address);
ps.setString(10,city);
ps.setString(11,state);
ps.setString(12,country);
int i = ps.executeUpdate();
if (i>0){
x=true;
}
out.print("registered succcessfully");
//out.close();
}
catch(Exception e)
{
out.print("error");
//out.close();
}
return x;
}
XML页面
<display-name>MobileWorld</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>mbloginservlet</display-name>
<servlet-name>mbloginservlet</servlet-name>
<servlet-class>mbworld.mbloginservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mbloginservlet</servlet-name>
<url-pattern>/mbloginservlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>Validate</display-name>
<servlet-name>Validate</servlet-name>
<servlet-class>mbworld.Validate</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Validate</servlet-name>
<url-pattern>/Validate</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>success</display-name>
<servlet-name>success</servlet-name>
<servlet-class>mbworld.success</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>success</servlet-name>
<url-pattern>/success</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>mbregistrationservlet</display-name>
<servlet-name>mbregistrationservlet</servlet-name>
<servlet-class>mbworld.mbregistrationservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mbregistrationservlet</servlet-name>
<url-pattern>/mbregistrationservlet</url-pattern>
</servlet-mapping>
如果我做错了,请告诉我.......
答案 0 :(得分:2)
将方法名称更改为doPost而不是register(不带static关键字),并使用getParameter api使用http servlet请求访问表单字段。所以它看起来像:
public class mbregistrationservlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String firstName = request.getParameter("firstfame");
//and so on
}
答案 1 :(得分:2)
我没有在servlet中看到一个doPost()方法,当你提交表单时,它实际上将由服务方法调用。
doPost()方法的结构
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// Servlet code
}
<强>更正强>
您不会像您期望的那样通过参数获取输入字段register method
public static boolean register(...)
更多,这是非法的。
在,为了获取输入字段参数user Enumeration params = request.getParameterNames()
,它将返回所有参数的枚举,并通过request.getParameterValue(params.nextElement())
,您可以访问与{{指定的输入字段对应的值1}}。
EXAMPLE
答案 2 :(得分:2)
首先,您必须使用doPost方法编写代码,第二件事是,如果您在rd.forward(request,response)
之后未使用RequestDispatcher rd=request.getRequestDispatcher("Name of page you want to redirect");
,则在单击“提交”按钮后它将成为空白页。 / p>
所以应该是这样的:
RequestDispatcher rd=request.getRequestDispatcher("Welcome");
rd.forward(request,response);