0

我目前正在使用教程(http://www.java-programming.info/tutorial/pdf/csajsp2/07-Cookies.pdf)来尝试启用cookie创建,我的servlet在下面的代码中,但是我想要添加一个 href 或按钮,以便当用户受到欢迎时,他们可以单击并被带到我的 webContent 文件夹中的 jsp ,我正在使用 jsp 、 eclipse indigo 和 tomcat 6 ,任何提示都会真正帮助添加 href 为如下所示不起作用,顺便说一句,代码中的 w3schools 链接可以忽略

package The_Quiz;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

/** Servlet that processes a registration form containing
 *  a user's first name, last name, and email address.
 *  If all the values are present, the servlet displays the
 *  values. If any of the values are missing, the input
 *  form is redisplayed. Either way, the values are put
 *  into cookies so that the input form can use the
 *  previous values.
 *  <p>
 *  From <a href="http://courses.coreservlets.com/Course-Materials/">the
 *  coreservlets.com tutorials on servlets, JSP, Struts, JSF, Ajax, GWT, and Java</a>.
 */

 public class RegistrationServlet extends HttpServlet {
  @Override
  public void doGet(HttpServletRequest request,
                HttpServletResponse response)
  throws ServletException, IOException {
 response.setContentType("text/html");
 boolean isMissingValue = false;
 String firstName = request.getParameter("firstName");
 if (isMissing(firstName)) {
  firstName = "Missing first name";
  isMissingValue = true;
 }
 String lastName = request.getParameter("lastName");
    if (isMissing(lastName)) {
  lastName = "Missing last name";
  isMissingValue = true;
 }
 String emailAddress = request.getParameter("emailAddress");
 if (isMissing(emailAddress)) {
  emailAddress = "Missing email address";
  isMissingValue = true;
 }
 Cookie c1 = new LongLivedCookie("firstName", firstName);
 response.addCookie(c1);
 String formAddress =
  "The_Quiz.RegistrationForm";
       if (isMissingValue) {
  response.sendRedirect(formAddress);
    } else {
  PrintWriter out = response.getWriter();
  String docType =
    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
    "Transitional//EN\">\n";
  String title = "Thanks for Registering";
  out.println
    (docType +
     "<HTML>\n" +
     "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
     "<BODY BGCOLOR=\"#FDF5E6\">\n" +
     "<CENTER>\n" +
     "<H1 ALIGN>" + title + "</H1>\n" +
     "<UL>\n" +
     "  <LI><B>First Name</B>: " +
            firstName + "\n" +
     "</UL>\n" +
     "<a href="Question_1.jsp">Visit W3Schools</a>" +       
     "</CENTER></BODY></HTML>");
     }
   }

  /** Determines if value is null or empty. */

  private boolean isMissing(String param) {
   return((param == null) ||
       (param.trim().equals("")));
        }
        }
4

2 回答 2

0

你添加了一个 cookie,但你得到了一个请求参数。您应该CookieUtilities根据书中的示例创建并使用CookieUtilities.getCookieValue 如果您想获取参数 - 您需要在您的 URL 中发送请求参数

于 2013-04-06T16:23:03.993 回答
0

cookie的创建应该是这样的

Cookie c = new Cookie('cookiename','value');

formAddress 的第二件事,您应该像这样指定 url

/RegistrationForm 

不要给出包名。

是的,对于锚标签,用单引号指定jsp的url

于 2013-04-06T16:24:18.977 回答