我目前正在使用教程(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("")));
}
}
答案 0 :(得分:0)
您添加了Cookie,但是您获得了一个请求参数。
您应该根据书中的示例创建CookieUtilities
并使用CookieUtilities.getCookieValue
如果您想获得参数 - 您需要在URL中发送请求参数
答案 1 :(得分:0)
cookie的创建应该是这样的
Cookie c = new Cookie('cookiename','value');
formAddress的第二件事你应该像这样指定url
/RegistrationForm
不要给包名。
是的,对于锚标记在单引号中指定jsp的url