回 帖 发 新 帖 刷新版面

主题:jsp与servlet传值问题

以下是JSP页面代码
--------------------------------------------------------------------------
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>虚拟卡调用日志</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  <form method="post" action="XnkMain">
    <table>
       <tr>
         <td><input type="submit" value="查询"></td>
         <td><input type="reset" value="清除"></td>
       </tr>
       <tr>
         <td>编号:</td><td><input type="text" name=x1></td>
       </tr>
    </table>
  </form>
  <table>
    <tr>
       <td>申请编号</td><td>申请密码</td><td>测试点信息</td><td>测试终端IP</td><td>测试终端通道</td><td>申请业务类型编码</td>
       <td>卡号</td><td>运营商代码</td><td>归属省</td><td>归属市</td><td>申请结果</td><td>IMSI</td>
       <td>SIM复用器信息</td><td>SIM复用地址IP</td><td>远程预订时间</td><td>远程注册时间</td><td>远程释放时间</td>
    </tr>
   </table>
</html>

------------------------------------------------------------------------------------
************************************************************************************
------------------------------------------------------------------------------------
以下是servlet代码
------------------------------------------------------------------------------------
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class XnkMain extends HttpServlet 
{
    public XnkMain()
    {
        super();
    }
    public void destroy()
    {
        super.destroy();
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print("    This is ");
        out.print(this.getClass());
        out.println(", using the GET method");
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }
    public void init() throws ServletException 
    {
    }

}
-------------------------------------------------------------------------------
请问一下有谁知道我那里错了吗?????

回复列表 (共2个回复)

沙发

<form method="post" action="XnkMain">

你在JSP中指定的提交方式为 post ,所以在 Servlet中必须以 doPost方法处理该请求。

public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print("    This is ");
        out.print(this.getClass());
        out.println(", using the GET method");
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

还有一个方法就是在doPost 中调用 doGet方法
public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doGet(request,response);
    }

板凳

servlet应该是这样的:
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class XnkMain extends HttpServlet 
{
    public XnkMain()
    {
        super();
    }
    public void destroy()
    {
        super.destroy();
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print("    This is ");
        out.print(this.getClass());
        out.println(", using the GET method");
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }
    public void init() throws ServletException 
    {
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doGet(request,response);
    }

}
程序结果是:This is class XnkMain, using the GET method

我来回复

您尚未登录,请登录后再回复。点此登录或注册