Cấu hình file jsp trong web.xml

Cấu hình file jsp trong web.xml ,đây là nội dung bài viết này tôi sẽ hướng dẫn các bạn cụ thể từ a – z như thế nào nhé.

Cách làm thông thường

Thông thường để cấu hình file jsp ví dụ như đường dẫn trên url khi chạy trực tiếp từ file jsp,chúng ta cần tạo ra một file servlet sau đó tiến hành mapping trong file xml và sau đó chuyển tiếp đến trang jsp.Như vậy sẽ dấu được được đường dẫn url của file jsp.

Code như sau :

1.Cấu hình trong file servlet HelloWorldController.java :

package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HelloWorldController
 */
public class HelloWorldController extends HttpServlet {
 private static final long serialVersionUID = 1L;
 
 /**
 * @see HttpServlet#HttpServlet()
 */
 public HelloWorldController() {
 super();
 }

 /**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 doPost(request, response);
 }

 /**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
 dispatcher.forward(request, response);
 }

}

2.Cấu hình trong file web.xml:

<servlet>
	<servlet-name>HelloWorldController</servlet-name>
	<servlet-class>controller.HelloWorldController</servlet-class>
</servlet>

<servlet-mapping>
	<servlet-name>HelloWorldController</servlet-name>
	<url-pattern>/helloWorld</url-pattern>
</servlet-mapping>

Khi chạy đường dẫn của chúng ta sẽ là /helloWorld.Ok. Bây giờ chúng ta cũng có thể cấu hình đường dẫn như vậy cho file jsp trong web.xml.

Cấu hình file jsp trong file web.xml

Các bạn khai báo trong file web. xml :

<servlet>
<servlet-name>myjsp</servlet-name>
<jsp-file>/myjsp.jsp</jsp-file>
<init-param>
<param-name>hello</param-name>
<param-value>test</param-value>
</init-param>
</servlet>
 
<servlet-mapping>
<servlet-name>myjsp</servlet-name>
<url-pattern>/url-pattern</url-pattern>
</servlet-mapping>

 

Đoạn code trên chúng ta quan tâm tới url-pattern đó là đường dẫn url bạn muốn config,jsp-file : trỏ tới đường dân file jsp của bạn .

Qua đó,các bạn cũng có thể config các giá trị mặc định ở file jsp này,bằng cách sử dụng param-name,đây là tên param bạn đặt tùy ý,dự vào param này để lấy giá trị mà bạn đã gán ở param-value.

Như vậy,tại file jsp muốn lấy giá trị ,các bạn chỉ cần khai báo :

<%= config.getInitParameter(“hello”)%>

Config là đối tượng tiềm ẩn trong jsp.Dùng phương thức getInitParameter truyền vào param đã config.

Chúc các bạn thực hiện thành công!

0 0 đánh giá
Đánh giá bài viết
Theo dõi
Thông báo của
guest
0 Góp ý
Phản hồi nội tuyến
Xem tất cả bình luận
x