Đối tượng config trong JSP

Đối tượng config trong JSP

Đối tượng config trong JSP

Learn more »

Đối tượng config trong JSP



Đối tượng config trong JSP là đối tượng ẩn của javax.servlet.ServletConfig.

Đối tượng này cho phép lập trình viên JSP truy cập vào các tham số khởi tạo Servlet hoặc JSP engine như đường dẫn hoặc vị trí tập tin, vv.

Ví dụ phương thức sau trả về tên servlet, là chuỗi chứa trong phần tử <servlet-name> được định nghĩa trong file WEB-INF\web.xml.

 config.getServletName(); 

Nói chung, nó được sử dụng để lấy ra các tham số khởi tạo từ file cấu hình web.xml.


Ví dụ về đối tượng config trong JSP

Tạo trang index.html

 <form action="welcome">   <input type="text" name="username">    <input type="submit" value="Submit"> </form> 

Tạo trang welcome.jsp

 <%     out.print("Welcome " + request.getParameter("username"));      String driver = config.getInitParameter("dname");     String servlet = config.getServletName();     out.print("<br> Driver name is = " + driver);     out.print("<br> Servlet name is = " + servlet); %> 

Cấu hình file web.xml

 <servlet>     <servlet-name>welcome</servlet-name>     <jsp-file>/welcome.jsp</jsp-file>      <init-param>         <param-name>dname</param-name>         <param-value>com.mysql.jdbc.Driver</param-value>     </init-param> </servlet>  <servlet-mapping>     <servlet-name>welcome</servlet-name>     <url-pattern>/welcome</url-pattern> </servlet-mapping> 

Kết quả:

Đối tượng config trong JSP

Click Submit.

Đối tượng config trong JSP


Lượt xem : 301

Integrations
Users

Share Profile

Anyone at KeenThemes can view
Anyone with link can edit

Give Award

Anyone at KeenThemes can view
Anyone with link can edit

Report User

Let us know why you’re reporing this person
Don't worry, your report is completely anonymous; the person you're
reporting will not be informed that you've submitted it