0
我已經嘗試了一個使用JSP的示例,因爲我試圖在web.xml中聲明的JSP中獲取初始參數。但它返回空值。config.getInitParameter()在JSP中返回空值?
見下面我的代碼: Response_Config.html
<html>
<head>
<meta charset="ISO-8859-1">
<title>Response and Config Object Example</title>
</head>
<body>
<form action="Imp_response.jsp">
User Name : <input type="text" name="username" /><br />
<input type="submit" value="Go"/>
</form>
</body>
</html>
Imp_response.jsp
<body>
<%
System.out.println("Before Redirecting");
String uname = request.getParameter("username");
session.setAttribute("username", uname);
response.sendRedirect("Imp_config.jsp"); %>
</body>
Imp_config.jsp
<body>
<%
out.println("Welcome !... "+session.getAttribute("username"));
String uname=config.getInitParameter("company");
String degree = config.getInitParameter("degree");
System.out.println("uname = "+uname);
System.out.println("degree = "+degree);
%>
<h4>Company : <%= uname %></h4><br />
<h4>Degree : <%= degree %></h4>
</body>
的web.xml
<servlet>
<servlet-name>ImplicitConfig</servlet-name>
<jsp-file>/Imp_config.jsp</jsp-file>
<init-param>
<param-name>company</param-name>
<param-value>ABC pvt ltd</param-value>
</init-param>
<init-param>
<param-name>degree</param-name>
<param-value>MCA</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ImplicitConfig</servlet-name>
<url-pattern>/Imp_config</url-pattern>
</servlet-mapping>
申請流程是:Response_Config.html - > Imp_response.jsp - > Imp_config.jsp
當我運行它作爲整個應用程序,它提供了以下的輸出: 在我的網頁
Welcome !... kavi //I have entered User Name : kavi
Company : null
Degree : null
但是,當我運行Imp_config.jsp單獨它提供了以下的輸出:
Welcome !... null //I know the session is null, when I run separately
Company : ABC pvt ltd
Degree : MCA
我不知道爲什麼它產生空值,當我作爲整個應用程序運行它。請分享您的想法,這將是欣賞。謝謝
謝謝@Gurkan Yesilyurt –