JSP 中Servlet的自己實現
jsp 中servlet的自己實現
一 步驟
1、繼承httpservlet
2、重寫doget()或者dopost()方法
3、在web.xml中注冊servlet
二 httpservlet類繼承圖
三 實例
1、創建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>
四 運行結果
如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
- jsp+servlet實現文件上傳與下載功能
- EJB3.0部署消息驅動Bean拋javax.naming.NameNotFoundException異常
- 在JSP中使用formatNumber控制要顯示的小數位數方法
- 秒殺系統Web層設計的實現方法
- 將properties文件的配置設置為整個Web應用的全局變量實現方法
- JSP使用過濾器防止Xss漏洞
- 在JSP頁面中動態生成圖片驗證碼的方法實例
- 詳解JSP 內置對象request常見用法
- 使用IDEA編寫jsp時EL表達式不起作用的問題及解決方法
- jsp實現局部刷新頁面、異步加載頁面的方法
- Jsp中request的3個基礎實踐
- JavaServlet的文件上傳和下載實現方法
- JSP頁面的靜態包含和動態包含使用方法