您的位置:首页 > 编程语言 > Java开发

Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之12.Servlet基础(3)

2009-06-23 20:52 931 查看

[b]–提交表单的方法
• get
• post

–Servlet 生命周期
–使用Servlet 输出HTML页面
–获得Servlet初始化参数
–页面导航
• 请求重定向
–response.sendRedirect(“url”);

• 请求转发
–request.getRequestDispatcher(“url”).forward(request,response);
• 请求包含
–request.getRequestDispatcher(“url”).include(request,response);
############Michael分割线################[/b]
请求重定向
ServletBasic.java

package com.michael.servlet;

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 ServletBasic extends HttpServlet {

/**
* Constructor of the object.
*/
public ServletBasic() {
super();
System.out.println("-----ServletBasic-----");
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy();
System.out.println("-----destroy-----");
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username="+username);
response.sendRedirect("http://redking.blog.51cto.com");
/*
if(username!=null&&username.equals("redking")){
request.getRequestDispatcher("/successfull.html").forward(request,response);
}else{
request.getRequestDispatcher("/failure.html").forward(request,response);
}
*/
/*
System.out.println("-----doGet-----");

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
*/
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("-----doPost-----");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
/*
//第一种方法
String driver = this.getServletContext().getInitParameter("driver");
String url = this.getServletContext().getInitParameter("url");
System.out.println(driver);
System.out.println(url);
//第二种方法
Enumeration enu = this.getServletContext().getInitParameterNames();
while(enu.hasMoreElements()){
String name = (String) enu.nextElement();
String value = this.getServletContext().getInitParameter(name);
System.out.println(name+":"+value);
}
*/
String username = this.getInitParameter("username");
String password = this.getInitParameter("password");
System.out.println(username);
System.out.println(password);
System.out.println("-----init-----");
}
/*
@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("-----service-----");
}
*/
}




看下效果



重定向了



############Michael分割线################
请求包含
ServletBasic.java

package com.michael.servlet;

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 ServletBasic extends HttpServlet {

/**
* Constructor of the object.
*/
public ServletBasic() {
super();
System.out.println("-----ServletBasic-----");
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy();
System.out.println("-----destroy-----");
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username="+username);
//请求重定向
//response.sendRedirect("http://redking.blog.51cto.com");
//请求包含
PrintWriter out = response.getWriter();
out.println("This is Servlet Page~~~");
request.getRequestDispatcher("/successfull.html").include(request,response);
/*
//请求转发
if(username!=null&&username.equals("redking")){
request.getRequestDispatcher("/successfull.html").forward(request,response);
}else{
request.getRequestDispatcher("/failure.html").forward(request,response);
}
*/
/*
System.out.println("-----doGet-----");

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
*/
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("-----doPost-----");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
/*
//第一种方法
String driver = this.getServletContext().getInitParameter("driver");
String url = this.getServletContext().getInitParameter("url");
System.out.println(driver);
System.out.println(url);
//第二种方法
Enumeration enu = this.getServletContext().getInitParameterNames();
while(enu.hasMoreElements()){
String name = (String) enu.nextElement();
String value = this.getServletContext().getInitParameter(name);
System.out.println(name+":"+value);
}
*/
String username = this.getInitParameter("username");
String password = this.getInitParameter("password");
System.out.println(username);
System.out.println(password);
System.out.println("-----init-----");
}
/*
@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("-----service-----");
}
*/
}




测试






############Michael分割线################

附件:http://down.51cto.com/data/2353150
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐