Commit 174d07c5 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

9780596005726

0 → 120000
+1 −0
Changes for 9780596005726: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
9780596005726
 No newline at end of file

README.md

0 → 100644
+15 −0
Changes for README.md: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
## Example files for the title:  
  
# Java Servlet & JSP Cookbook, by Bruce Perry
  
[![Java Servlet & JSP Cookbook, by Bruce Perry](http://akamaicovers.oreilly.com/images/9780596005726/cat.gif)](https://www.safaribooksonline.com/library/view/title/0596005725//)
  
The following applies to example files from material published by O’Reilly Media, Inc. Content from other publishers may include different rules of usage. Please refer to any additional usage rights explained in the actual example files or refer to the publisher’s website.
  
O'Reilly books are here to help you get your job done. In general, you may use the code in O'Reilly books in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from our books does not require permission. Answering a question by citing our books and quoting example code does not require permission. On the other hand, selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Incorporating a significant amount of example code from our books into your product's documentation does require permission.
  
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN.
  
If you think your use of code examples falls outside fair use or the permission given here, feel free to contact us at <permissions@oreilly.com>.
  
Please note that the examples are not production code and have not been carefully testing. They are provided "as-is" and come with no warranty of any kind.
+94 −0
Changes for jspcookbook/chap1/com/jspservletcookbook/FirstServlet.java: 94 added lines, 0 removed lines.
Original line number Diff line number Diff line
package com.jspservletcookbook;

import java.io.IOException;
import java.io.PrintWriter;

import java.util.Enumeration;

import javax.servlet.ServletException;

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

public class FirstServlet extends HttpServlet {

  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {

	System.out.println("command: "+request.getParameter("command"));

	//set the MIME type of the response, "text/html"
    response.setContentType("text/html");

	//use a PrintWriter send text data to the client who has requested the servlet
    java.io.PrintWriter out = response.getWriter();

	//Begin assembling the HTML content
    out.println("<html><head>");

    out.println("<title>Help Page</title></head><body>");
    out.println("<h2>Please submit your information</h2>");

   //make sure method="post" so that the servlet service method
   //calls doPost in the response to this form submit
    out.println(
        "<form method=\"post\" action =\"" + request.getContextPath() +
            "/firstservlet\" >");

    out.println("<table border=\"0\"><tr><td valign=\"top\">");
    out.println("Your first name: </td>  <td valign=\"top\">");
    out.println("<input type=\"text\" name=\"firstname\" size=\"20\">");
    out.println("</td></tr><tr><td valign=\"top\">");
	out.println("Your last name: </td>  <td valign=\"top\">");
    out.println("<input type=\"text\" name=\"lastname\" size=\"20\">");
    out.println("</td></tr><tr><td valign=\"top\">");
    out.println("Your email: </td>  <td valign=\"top\">");
    out.println("<input type=\"text\" name=\"email\" size=\"20\">");
    out.println("</td></tr><tr><td valign=\"top\">");

    out.println("<input type=\"submit\" value=\"Submit Info\"></td></tr>");
    out.println("</table></form>");
    out.println("</body></html>");
     } //end doGet

	 public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException,java.io.IOException{

	//display the parameter names and values
	Enumeration paramNames = request.getParameterNames();

	String parName;//this will hold the name of the parameter from the HTML form

	boolean emptyEnum = false;
	if (! paramNames.hasMoreElements())
	    emptyEnum = true;


	//set the MIME type of the response, "text/html"
    response.setContentType("text/html");

    //use a PrintWriter send text data to the client who has requested the servlet
    java.io.PrintWriter out = response.getWriter();

	//Begin assembling the HTML content
    out.println("<html><head>");
    out.println("<title>Submitted Parameters</title></head><body>");

	if (emptyEnum){
	    out.println("<h2>Sorry, the request does not contain any parameters</h2>");
	} else {
	    out.println("<h2>Here are the submitted parameter values</h2>");
	}

	while(paramNames.hasMoreElements()){

	    parName = (String) paramNames.nextElement();
	    out.println("<strong>" + parName + "</strong> : " + request.getParameter(parName));
	    out.println("<br />");
	}//while

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

	}// doPost
}
+11 −0
Changes for jspcookbook/chap1/firstJsp.jsp: 11 added lines, 0 removed lines.
Original line number Diff line number Diff line
<%-- use the 'taglib' directive to make the JSTL 1.0 core tags available --%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%-- use the 'jsp:useBean' standard action to make the Date object available in page scope --%>
<jsp:useBean id="date" class="java.util.Date" />
<html>
<head><title>First JSP</title></head>
<body>
<h2>Here is today's date</h2>
<c:out value="Date: ${date}" />
</body>
</html>
 No newline at end of file
+84 −0
Changes for jspcookbook/chap10/com/jspservletcookbook/CookieBean.java: 84 added lines, 0 removed lines.
Original line number Diff line number Diff line
package com.jspservletcookbook;           

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Cookie;

public class CookieBean {

  private Cookie cookie = null;

public CookieBean(){}

//set the cookie name
public void setName(String name){

    if (name == null || (name.equals("")))
        throw new IllegalArgumentException("Invalid cookie name set in: "+getClass().getName());
    
    cookie = new Cookie(name,""+new java.util.Date().getTime());
}

//set the cookie value
public void setValue(String value){

 if (value == null || (value.equals("")))
        throw new IllegalArgumentException("Invalid cookie value set in: "+getClass().getName());
    
    if (cookie != null)
        cookie.setValue(value);

}

public void setMaxAge(int maxAge){
    
    if (cookie != null)
        cookie.setMaxAge(maxAge);

}

public void setPath(String path){

 if (path == null || (path.equals("")))
        throw new IllegalArgumentException("Invalid cookie path set in: "+getClass().getName());
    
    if (cookie != null)
        cookie.setPath(path);
}

public void setCookieHeader(HttpServletResponse response){

    if (response == null )
        throw new IllegalArgumentException("Invalid HttpServletResponse set in: "+getClass().getName());
    if (cookie != null)
        response.addCookie(cookie);
}

public String getName(){

    if (cookie != null)
       return cookie.getName();
    else
       return "unavailable";
       
       }
       
public String getValue(){

    if (cookie != null)
       return cookie.getValue();
    else
       return "unavailable";
       
       }
       
public String getPath(){

    if (cookie != null)
       return cookie.getPath();
    else
       return "unavailable";
       
       }


}
 No newline at end of file
Loading