JavaEE 5开发指南学习(创建和部署jsp的web应用2)
下面是一个使用标准操作和内置对象的例子。这个例子扩展了上一个例子,增加了一个注册页面。使用JavaBean,还是用内置对象request来读取请求参数。
目录结构如下:
首先创建一个JavaBean。
User.java
package com.apress.faq;public class User {private String firstName;private String surname;private String loginName;private int age;...get,set方法}}
<%@ page errorPage="/WEB-INF/errorPage.jsp" import = "java.util.*,com.apress.faq.*" %><html><head><title>Java FAQ Welcome page!</title></head><body><%User user =(User)session.getAttribute("user");if(user==null){%>You are not registered!please<a href = "register.html"> Register</a><%} else{%>Welcome to the java FAQ.<%! FaqCategories f = new FaqCategories(); %>click a link below for answers to the given topic.<%Iterator it = f.getAllCategories();while(it.hasNext()){String s = (String)it.next();%><p><a href="<%=replaceUnderscore(s) %>.jsp"><%=s %></a></p><% } %><%@ include file = "WEB-INF/footer.jspf" %><% } %></body></html><%!public String replaceUnderscore(String s){ return s.replace(' ','_'); }%>
<html><head><title>Registeration page</title></head><body><form action="register.jsp" method="POST"><table><tr><td align="right">First name:</td><td align="left"><input type="text" name="firstName"></td></tr><tr><td align="right">sur name:</td><td align="left"><input type="text" name="surname"></td></tr><tr><td align="right">Login name:</td><td align="left"><input type="text" name="loginName"></td></tr><tr><td align="right">Age:</td><td align="left"><input type="text" name="firstName"></td></tr></table>which topic are you interested in?<br><input type="checkbox" name="topics" value="hobby">hobby</input><br><input type="checkbox" name="topics" value="study">study</input><br><input type="checkbox" name="topics" value="thread">thread</input><p><input type = "submit" value="Submit" /></p></form></body></html>
<html><head><title>register user!</title></head><body><jsp:useBean id="user" scope="session" property="*" /></jsp:useBean>welcome new user,these are the values you submited:<p>Your firstname is<%=user.getFirstName() %>.</p><p>Your surname is<%=user.getSurname() %>.</p><p>Your loginName is<%=user.getLoginName() %>.</p><p>Your age is<%=user.getAge() %>.</p>You selected these topics:<%String[] topics = request.getParameterValues("topics");if(topics==null){topics=new String[]{"No topics"};}for(int i =0;i<topics.length;i++){%><br><%=topics[i] %><%}%><p>Go to <a href="welcome.jsp">Topic List page</a></p><%@ include file = "WEB-INF/footer.jspf" %></body></html>
package org.apache.jsp;import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;import java.util.*;import com.apress.faq.*;下面是class语句
public final class welcome_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent {
public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { PageContext pageContext = null; HttpSession session = null; ServletContext application = null; ServletConfig config = null; JspWriter out = null; Object page = this; JspWriter _jspx_out = null; PageContext _jspx_page_context = null;在servlet中与之对应的方法是service()。