Javaweb当中对Servlet中的doget和dopost方法的简单使用

来源:互联网 发布:vb数据库源代码 编辑:程序博客网 时间:2024/06/06 03:41
<html>  <head>    <base href="<%=basePath%>">        <title>使用myeclipse进行servlet程序的创建操作 </title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page">  </head>    <body>    <h2>使用myeclipse进行Servlet程序的访问操作</h2>    <hr>    <!--通过超链接进行的请求方式为get方法请求-->    <a href="servlet/HelloServlet">采用get方式进行Servlet请求操作</a>    <form action="servlet/HelloServlet" method="post">    <input type="submit" value="采用post方式进行Servlet请求操作">    </form>  </body></html>

上述为一个jsp文件代码。

通过href超链接进行请求的访问时,将会采用get方式将数据信息发送到所制定的Servlet对象当中的doGet方法中来进行数据的处理操作

form表当当中设置了提交的方法为post方式进行请求的发送之后,请求数据将会同过制定的Servlet方法当中的dopost方法来进行数据的请求操作

package servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class HelloServlet extends HttpServlet {/** * Constructor of the object. */public HelloServlet() {super();}/** * Destruction of the servlet. <br> */public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {System.out.println("进行doget方法的重写操作,用于处理get请求");response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("<HTML>");out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println("  <BODY>"); out.print("<Strong>HelloServlet</Strong>");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方法的重写操作,用去处理post请求你当中的信息");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("<Strong>HelloServlet</Strong>");out.print(this.getClass());out.println(", using the POST method");out.println("  </BODY>");out.println("</HTML>");out.flush();out.close();}}
上述为一个Servlet程序代码,在该代码当中进行了doget和dopost方法的重写操作。用于处理客户端所发来的get类型的请求和post类型的请求。

下面则是一个对应的web.xml文件、该文件是一个配置文件,在服务器启动运行时将会被加载。

在服务器运行时,将会将jsp文件当中的href和action当中的地址参数值拿来与web.xml当中<url-pattern>标签当中的参数值来进行比对,当二者相同时,将会通过映射servlert-name标签当中的参数值来找到<servlet-name>标签当中参数名字相同的映射,然后访问<servlet-class>标签当中的参数路径值,来对指定路径下的Servlet程序进行访问操作

0 0
原创粉丝点击