学习JSP(二)--JSP

学习JSP(二)--JSP
一 JSP与Servlet的关系

  JSP在首次运行时被编译成Servlet,再编译成.class文件。所以说,JSP就是Servlet。
        
        实例:Client -> Servlet -> java class ->Servlet -> Client

  1 b.html
<! DOCTYPE html PUBLIC  " -//W3C//DTD HTML 4.01 Transitional//EN "   " http://www.w3.org/TR/html4/loose.dtd " >
< html >
< head >
< meta http - equiv = " Content-Type "  content = " text/html; charset=UTF-8 " >
< title > 表单信息 </ title >
</ head >
< body >
< h1 align = " center " > 表单信息 </ h1 >
< form id = " form1 "  name = " form1 "  method = " post "  action = " b " >
      id: 
< input name = " id "  type = " text "  id = " id "   />< br />
    password: 
< input name = " password "  type = " password "  id = " password "   />< br />
    content:
< textarea name = " content "  id = " content " ></ textarea >< br />
    
< label > sex: < br  />
        
< input name = " sex "  type = " radio "  value = " man "  checked = " checked "   /> man < br />
        
< input type = " radio "  name = " sex "  value = " women "   /> women < br />
    age:
< select name = " age "  id = " age " >
      
< script type = " text/javascript " >
        
for (var i = 1 ;i < 20 ;i ++ ) {
            document.write(
"<option value='"+i+"'>"+i+"</option>");
        }

        
</ script >
    
</ select >
    
< br />
    
< center >< input type = " submit "  name = " Submit "  value = " 提交 "   />
</ form >
</ body >
</ html >

  2 b对象的类

package  servlet;

public   class  BClass  {
    
private String id;
    
private String password;
    
private String content;
    
private int age;
    
private String sex;

//set() and get()
  

}


  3 b对象的处理类

package  servlet;

public   class  BClassFactory  {
    
public String doBClass(BClass bClass) {
        
return "I'm " + bClass.getId() + "!";
    }

}


  4 Servlet

package  servlet;

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

public   class  BServlet  extends  HttpServlet  {
    @Override
    
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BClass bClass 
= new BClass();
        bClass.setId(req.getParameter(
"id"));
        bClass.setAge(Integer.parseInt(req.getParameter(
"age")));
        bClass.setContent(req.getParameter(
"content"));
        bClass.setSex(req.getParameter(
"sex"));
        bClass.setPassword(req.getParameter(
"password"));

        BClassFactory factory 
= new BClassFactory();
        String doBClass 
= factory.doBClass(bClass);

        req.setAttribute(
"doBClass", doBClass);
        req.setAttribute(
"bClass", bClass);

        req.getRequestDispatcher(
"b.jsp").forward(req, resp);
    }

}


  其中,"req.getRequestDispatcher("b.jsp").forward(req, resp);" 是将结果转发到另一个url 上的指令。除了forward,也可以用redirect来完成这个功能。

   RequestDispatcher.forward()方法和HttpServletResponse.sendRedirect()方法的区别是:前者仅是容器中控制权的转向,在客户端浏览器地址栏中不会显示出转向后的地址,他是不会改变Request的值,如果你需要在下一个页面中能从中获取新的信息的话,你可以Request.setAttribute()来放置一些标志,这样从下一个页面中获取;后者则是完全的跳转,浏览器将会得到跳转的地址,并重新发送请求链接。这样,从浏览器的地址栏中可以看到跳转后的链接地址。所以,前者更加高效,在前者可以满足需要时,尽量使用Request Dispatcher.forward()方法,并且,这样也有助于隐藏实际的链接。在有些情况下,比如,需要跳转到一个其它服务器上的资源,则必须使用HttpServletResponse.sendRequest()方法。

  5 提交的页面b.jsp


< table width = " 500 "  border = " 0 "  cellpadding = " 0 "  cellspacing = " 0 "  bgcolor = " #FFCCFF " >
    
< tr >
        
< td colspan = " 2 " > $ {doBClass} </ td >
    
</ tr >
    
< tr >
        
< td width = " 92 " > id: </ td >
        
< td width = " 408 " > $ {bClass.id } </ td >
    
</ tr >
    
< tr >
        
< td > password: </ td >
        
< td > $ {bClass.password } </ td >
    
</ tr >
    
< tr >
        
< td > age: </ td >
        
< td > $ {bClass.age } </ td >
    
</ tr >
    
< tr >
        
< td > content: </ td >
        
< td > $ {bClass.content } </ td >
    
</ tr >
    
< tr >
        
< td > sex: </ td >
        
< td > $ {bClass.sex } </ td >
    
</ tr >
</ table >

  其中," ${}"在JSP2.0以后可以直接使用。

你可能感兴趣的:(学习JSP(二)--JSP)