gpt4 book ai didi

java - Java 中 ServletContext 的输出没有达到预期?

转载 作者:行者123 更新时间:2023-11-28 23:26:21 25 4
gpt4 key购买 nike

我正在尝试从 web.xml 文件中读取某些值,但在这个问题底部的图像中出现错误。

Config.html

<form action="go" method="get">
Enter name:<input type="text" name="pname"><br>
Enter Age:<input type="text" name="page"><br>
<input type="submit" value="submit">
</form>

UseServletContext.java

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{
PrintWriter pw=response.getWriter();

//Read form data through Form Page:
String name=request.getParameter("pname");
String age=request.getParameter("page");

//Approach1
ServletConfig cg=getServletConfig();
ServletContext sc=cg.getServletContext();

String db2url=sc.getInitParameter("db2url");
String db2user=sc.getInitParameter("db2user");

String sql="insert into jalajclients(name,age) values(?,?)";

//Convert age to numeric values.
int age1=Integer.parseInt(age.trim());

//Store these Values to the DataBAse.
try {
Class.forName("com.ibm.db2.jcc.Db2Driver");
Connection con=DriverManager.getConnection(db2url,db2user,"786");

PreparedStatement ps=con.prepareStatement(sql);
ps.setString(1,name);
ps.setInt(2, age1);

int i=ps.executeUpdate();
pw.println(i);
} catch(Exception e) {
e.printStackTrace();
}

}

我收到以下错误:

enter image description here

web.xml

 <servlet>
<description></description>
<display-name>UseServletContext</display-name>
<servlet-name>UseServletContext</servlet-name>
<servlet-class>UseServletContext</servlet-class>
</servlet>

<context-param>
<param-name>db2url</param-name>
<param-value>jdbc:db2://localhost:50000/mydb1235</param-value>
</context-param>

<context-param>
<param-name>db2user</param-name>
<param-value>piyush</param-value>
</context-param>

<servlet-name>UseServletContext</servlet-name>
<url-pattern>/go1</url-pattern>
</servlet-mapping>

谁能指导我做错了什么?

最佳答案

您是否覆盖了 Servlet 中的 init 方法?如果是,那么不要忘记调用 super.init(config);,如下所示

public void init(ServletConfig config) throws ServletException {
super.init(config);
}

关于java - Java 中 ServletContext 的输出没有达到预期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36562695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com