小弟在家写一个隐藏表单!出现了些问题!还请各位大大帮忙解答一下!
代码如下!
main页面
package ch8;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class Main extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK ";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println( " <html> ");
out.println( " <head> <title> Main </title> </head> ");
out.println( " <body bgcolor=\ "#ffffff\ "> ");
out.println( " <form action= 'regist ' method= 'post '> ");
out.println( "用户名: <input type= 'text ' name= 'username ' > <br/> ");
out.println( "密码: <input type= 'password ' name= 'password ' > <br/> ");
out.println( "年龄: <input type= 'text ' name= 'age ' > <br/> ");
out.println( "地址: <input type= 'text ' name= 'address ' > <br/> ");
out.println( "性别: <input type= 'radio ' name= 'sex ' value= '男 '> 男 <input type= 'radio ' name= 'sex ' value= '女 '> 女 <br/> ");
out.println( "爱好: <input type= 'checkbox ' name= 'like ' value= '电影 '> 看电影 ");
out.println( " <input type= 'checkbox ' name= 'like ' value= '音乐 '> 听音乐 ");
out.println(
" <input type= 'checkbox ' name= 'like ' value= '游戏 '> 打游戏 <br> ");
out.println( " <input type= 'submit ' value= '提交 '> ");
out.println( " </form> ");
out.println( " <p> The servlet has received a " + request.getMethod() +
". This is the reply. </p> ");
out.println( " </body> ");
out.println( " </html> ");
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
注册页面
Regist页面
package ch8;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class Register extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK ";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
request.setCharacterEncoding( "GBK ");
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println( " <html> ");
out.println( " <head> <title> Register </title> </head> ");
out.println( " <body bgcolor=\ "#ffffff\ "> ");
String username = request.getParameter( "username ");
String password = request.getParameter( "password ");
int userage = Integer.parseInt(request.getParameter( "userage "));
String address = request.getParameter( "address ");
String usersex = request.getParameter( "sex "); //性别虽然在页面是多项目,但是提交过来最多只有一个值,不需要用数组来接受
String[] like = request.getParameterValues( "like "); //爱好能提交多项数据!要用数组的方式来接收
String s = request.getParameter( "like ");
out.println(s + " <br> ");
if ( "张三 ".equals(username)) { //假定张三是重名用户
out.println( "注册失败!用户存在重名.请重新注册 ");
out.println( " <form action= 'Register ' method= 'post '> ");
out.println( "用户名: <input typr= 'text ' name= 'username '> </br> ");
out.println( " <input type = 'hidden ' name= 'password ' value= ' " +
password + " '> </br> ");
out.println( " <input type = 'hidden ' name = 'ueserage ' value= ' " +
userage + " '> </br> ");
out.println( " <input type = 'hidden ' name = 'address ' value= ' " +
address + " '> </br> ");
out.println( " <input type= 'hidden ' name= 'usersex ' value= ' " + usersex +
" '> <br/> ");
System.out.println( "Regist.doGet方法: " + like);
if (like != null) {
for (int i = 0; i < like.length; i++) {
out.println( " <input type = 'hidden ' name= 'like ' value = ' " +
like[i] + " '> </br> ");
}
}
out.println( " <input type= 'submit ' value= '再次提交 '> ");
out.println( " </form> ");
}else
{
out.println( "恭喜您!注册成功 </br> ");
out.println( "用户名: "+username+ " </br> ");
out.println( "密码: "+password+ " </br> ");
out.println( "年龄: "+userage+ " </br> ");
out.println( "地址: "+address+ " </br> ");
out.println( "性别: "+usersex+ " </br> ");
out.println( "爱好 ");
if(like!=null)
{
for(int i = 0;i <like.length;i++)
{
out.println(like[i]+ "  ");
}
}
}
out.println( " </body> ");
out.println( " </html> ");
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
运行后我注册好了结果就出现了:
Help viewer error URL: Unable to open location: Document not found on server
可是自己找了半天也没找出什么错误!(我自己感觉代码是一点问题也没有)
可是就是注册不成功!还望各位高手帮我解答一下!小弟不胜感激!
[解决办法]
你的main页是 <form action= 'regist ' method= 'post '>
而你注册页是 <form action= 'Register ' method= 'post '>
看出区别了吗?
[解决办法]
这种视图操作还是放在JSP里面比较方便,servlet主要还是用来写控制器。