Servlet和cookie管理。看看我已经做过的事情

时间:2014-04-01 21:05:41

标签: java html jsp servlets cookies

我试图完成以下作业:

用户在两个输入字段上写下其名称和年龄。 编写一个servlet,它返回一个html页面,servlet能够理解该用户是成人还是未成年人。 这就是我所做的:

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

public class hmIsAdult extends HttpServlet {

 protected void doPost( HttpServletRequest request,
 HttpServletResponse response )
 throws ServletException, IOException
 {
 String firstName = request.getParameter( "firstname" );
 String the_age = request.getParameter( "age" );
 int age;
 try {
    age = Integer.parseInt(the_age);
 }
 catch (NumberFormatException e) {
    age = 0;
 }

 response.setContentType( "text/html" );
 PrintWriter out = response.getWriter();

 out.println( "<?xml version = \"1.0\"?>" );

 out.println( "<!DOCTYPE html PUBLIC \"-//W3C//DTD " +
 "XHTML 1.0 Strict//EN\" \"http://www.w3.org" +
 "/TR/xhtml1/DTD/xhtml1-strict.dtd\">" );

 out.println(
 "<html xmlns = \"http://www.w3.org/1999/xhtml\">" );

 out.println( "<head>" );
 out.println( "<title>My first servlet</title>" );
 out.println( "</head>" );

 out.println( "<body>" );
 out.println( "<h1>Hello " + firstName + ",</h1><br />" );

 if (age < 18) {
   out.println( "<h3>You are not an adult.</h3>" );
 }
 else {
   out.println( "<h3>You are an adult.</h3>" );
 }

 out.println( "</body>" );
 out.println( "</html>" );
 out.close();
 }
}

web.xml包含以下指令:

<servlet>
        <servlet-name>homeworkIsAdult</servlet-name>
        <display-name>homeworkIsAdult display name</display-name>
        <description>homeworkIsAdult description</description>
        <servlet-class>hmIsAdult</servlet-class>
</servlet>

<servlet-mapping>
        <servlet-name>homeworkIsAdult</servlet-name>
        <url-pattern>/homeworkIsAdult</url-pattern>
</servlet-mapping>

包含servlet调用的html文件是:

<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>My first homework using servlet</title>
 </head>

 <body>
 <form action = "/mydir/homeworkIsAdult" method = "post">

 <p><label>Please insert your first name, your age and then press the Submit button<br />
       <input type = "text" name = "firstname" />
       <input type = "number" value = "0" step = "1" name = "age" />
       <input type = "submit" value = "Submit" />
 </p></label>

 </form>
 </body>
 </html>

它按预期工作。

在下一步中我将使用cookie:servlet现在应该记住使用cookie的参数firstname和age。

然后,当用户是成年人时,servlet还需要询问其地址。同一个servlet应该使用另一个cookie重新记住该地址,并生成一个包含firstname,age和address的页面。

当用户是次要用户时,servlet应该将用户重定向(使用sendRedirect方法)到第二个servlet。第二个servlet应该读取cookie并写一条消息,例如&#34;亲爱的&#34; +名字+&#34;你无法访问这些内容&#34; 这就是我所做的。

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

public class hmIsAdultCookie extends HttpServlet {

 protected void doPost( HttpServletRequest request,
 HttpServletResponse response )
 throws ServletException, IOException
 {
 String firstName = request.getParameter( "firstname" );
 String the_age = request.getParameter( "age" );
 int age;
 try {
    age = Integer.parseInt(the_age);
 }
 catch (NumberFormatException e) {
    the_age = "0";
    age = 0;
 }

 // IS IT OK TO GET THE PARAMETER ADDRESS HERE !?!?!
 String address = request.getParameter( "address" );
 if (address != null) {
   Cookie cookie_address = new Cookie("address", address);
 }

 Cookie cookie_firstname = new Cookie("firstname", firstName);
 Cookie cookie_age = new Cookie("age", the_age);

 response.addCookie(cookie_firstname);
 response.addCookie(cookie_age);

 response.setContentType( "text/html" );
 PrintWriter out = response.getWriter();

 out.println( "<?xml version = \"1.0\"?>" );

 out.println( "<!DOCTYPE html PUBLIC \"-//W3C//DTD " +
 "XHTML 1.0 Strict//EN\" \"http://www.w3.org" +
 "/TR/xhtml1/DTD/xhtml1-strict.dtd\">" );

 out.println("<html xmlns = \"http://www.w3.org/1999/xhtml\">" );

 out.println( "<head>" );
 out.println( "<title>My first servlet</title>" );
 out.println( "</head>" );

 out.println( "<body>" );
 out.println( "<h1>Hello " + firstName + ",</h1><br />" );

 if (age < 18) {
   // How can I redirect directly and automatically to another servlet (hmIsMinor for example)?
   response.sendRedirect("homeworkIsMinor");

 }
 else {
   // The user should insert the address, it should be read and saved on another cookie by hmIsAdult
   // servlet and produce a page with a report containing firstname, age and address.
   out.println( "<form action = \"/mydir/homeworkIsAdult\" method = \"post\">" );
   out.println( "<p><label>Give ve also the address and press the Submit button again<br />" );
   out.println( "<input type = \"text\" name = \"address\" />" );
   out.println( "<input type = \"submit\" value = \"Submit\" /></p></label></form>" );
 }

 out.println( "</body>" );

 // end XHTML document
 out.println( "</html>" );
 out.close();
 }


 protected void doGet( HttpServletRequest request,
 HttpServletResponse response )
 throws ServletException, IOException
 {

 Cookie cookies[] = request.getCookies();

 response.setContentType( "text/html" );
 PrintWriter out = response.getWriter();

 out.println( "<?xml version = \"1.0\"?>" );
 out.println( "<!DOCTYPE html PUBLIC \"-//W3C//DTD " +
 "XHTML 1.0 Strict//EN\" \"http://www.w3.org" +
 "/TR/xhtml1/DTD/xhtml1-strict.dtd\">" );

 out.println("<html xmlns = \"http://www.w3.org/1999/xhtml\">" );

 out.println( "<head>" );
 out.println( "<title>My first servlet</title>" );
 out.println( "</head>" );

 out.println( "<body>" );

 if ( cookies != null && cookies.length != 0 ) {
   out.println( "<h3>You have cookies</h3>" );
   for (int ii = 0; ii < cookies.length; ii++)
       out.println( "cookie name: " + cookies[ii].getName() + " cookie value: " + cookies[ii].getValue() + "<br/>" );
 }
 else {
   out.println( "<h3>You don't have cookies</h3>" );
 }

 out.println( "</body>" );

 out.println( "</html>" );
 out.close();
 }

}

这是文件hmIsMinor.java,使用sendFedirect方法调用,如家庭作业所示。

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

public class hmIsMinor extends HttpServlet {

 protected void doGet( HttpServletRequest request,
 HttpServletResponse response )
 throws ServletException, IOException
 {
 Cookie cookies[] = request.getCookies();

 response.setContentType( "text/html" );
 PrintWriter out = response.getWriter();

 out.println( "<?xml version = \"1.0\"?>" );

 out.println( "<!DOCTYPE html PUBLIC \"-//W3C//DTD " +
 "XHTML 1.0 Strict//EN\" \"http://www.w3.org" +
 "/TR/xhtml1/DTD/xhtml1-strict.dtd\">" );

 out.println("<html xmlns = \"http://www.w3.org/1999/xhtml\">" );

 out.println( "<head>" );
 out.println( "<title>My first servlet</title>" );
 out.println( "</head>" );

 out.println( "<body>" );
 if ( cookies != null && cookies.length != 0 ) {
   out.println( "<h3>You have cookies</h3>" );
   for (int ii = 0; ii < cookies.length; ii++)
       out.println( "cookie name: " + cookies[ii].getName() + " cookie value: " + cookies[ii].getValue() + "<br/>" );
 }
 else {
   out.println( "<h3>You don't have cookies</h3>" );
 }

out.println( "</body>" );

 out.println( "</html>" );
 out.close();
 }

}

web.xml文件的内容

<servlet>
        <servlet-name>homeworkIsAdultCookie</servlet-name>
        <display-name>homeworkIsAdultCookie display name</display-name>
        <description>homeworkIsAdultCookie description</description>
        <servlet-class>hmIsAdultCookie</servlet-class>
</servlet>

<servlet>
        <servlet-name>homeworkIsMinor</servlet-name>
        <display-name>homeworkIsMinor display name</display-name>
        <description>homeworkIsMinor description</description>
        <servlet-class>hmIsMinor</servlet-class>
</servlet>

<servlet-mapping>
        <servlet-name>homeworkIsAdultCookie</servlet-name>
        <url-pattern>/homeworkIsAdultCookie</url-pattern>
</servlet-mapping>

<servlet-mapping>
        <servlet-name>homeworkIsMinor</servlet-name>
        <url-pattern>/homeworkIsMinor</url-pattern>
</servlet-mapping>

我已经添加了另一个html文件(homeworkIsAdultCookie.html)。

<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">



<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Processing get requests with data</title>
 </head>

 <body>
 <form action = "/mydir/homeworkIsAdultCookie" method = "post">

 <p><label>
 Type your first name and press the Submit button
<br />
       <input type = "text" name = "firstname" />
       <input type = "number" value = "0" step = "1" name = "age" />
       <input type = "submit" value = "Submit" />
 </p></label>

 </form>
 </body>
 </html>

重定向似乎有效,但当用户是成年人时,我无法解决问题。

请问你能帮帮我吗?

0 个答案:

没有答案