黄色电影一区二区,韩国少妇自慰A片免费看,精品人妻少妇一级毛片免费蜜桃AV按摩师 ,超碰 香蕉

JSP 中Servlet的自己實現(xiàn)

jsp 中servlet的自己實現(xiàn)

一 步驟

1、繼承httpservlet
2、重寫doget()或者dopost()方法
3、在web.xml中注冊servlet 

二 httpservlet類繼承圖

三 實例

1、創(chuàng)建myfirstservletdemo web project. 

2、編輯index.jsp

<%@ page language="java" import="java.util.*" contenttype="text/html; charset=utf-8"%>
<%
string path = request.getcontextpath();
string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";
%>
 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
 <head>
 <base href="<%=basepath%>" rel="external nofollow" >
 
 <title>my jsp 'index.jsp' starting page</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">
  <!--
  <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" >
  -->
 </head>
 
 <body>
 <h1>第一個servlet小例子</h1>
 <a href="servlet/helloservlet" rel="external nofollow" >get方式請求helloservlet</a>
 <form action="servlet/helloservlet" method="post">
    <input type="submit" value="post方式請求helloservlet">
 </form>
 </body>
</html>

3、編寫helloservlet

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;
 
//繼承于httpservlet
public class helloservlet extends httpservlet {
 
  @override
  protected void doget(httpservletrequest request, httpservletresponse response)
      throws servletexception, ioexception {
    // todo auto-generated method stub
    system.out.println("處理get()請求...");
    printwriter out = response.getwriter();
    response.setcontenttype("text/html; charset=utf-8");
    out.println("<strong>hello servlet!</strong><br>");
  }
 
  @override
  protected void dopost(httpservletrequest request, httpservletresponse response)
      throws servletexception, ioexception {
    system.out.println("處理post()請求...");
    printwriter out = response.getwriter();
    response.setcontenttype("text/html; charset=utf-8");
    out.println("<strong>hello servlet!</strong><br>");
  }
 
}

4、編寫web.xml

<?xml version="1.0" encoding="utf-8"?>
<web-app version="2.5"
  xmlns="http://java.sun.com/xml/ns/javaee";
  xmlns:xsi="http://www.w3.org/2001/xmlschema-instance";
  xsi:schemalocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd";>
 <display-name></display-name> 
 <welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <servlet>
  <servlet-name>helloservlet</servlet-name>
  <servlet-class>servlet.helloservlet</servlet-class>
 </servlet>
 <servlet-mapping>
   <servlet-name>helloservlet</servlet-name>
   <url-pattern>/servlet/helloservlet</url-pattern>
 </servlet-mapping>
</web-app>
 

四 運行結(jié)果 

如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章