gpt4 book ai didi

java - 如何使用 Java servlet 获取用户的动态输入而无需表单?

转载 作者:太空宇宙 更新时间:2023-11-04 09:50:11 24 4
gpt4 key购买 nike

请帮助我实现以下要求。我需要在 URL 中传递一个参数,该参数动态地从用户获取值并通过 select 语句或过程获取记录。

我尝试使用硬编码值,我不知道如何在其中传递变量。

下面是代码供您引用。

package notifprj;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import notifprj.ConnectionUtils;
import java.sql.CallableStatement;
import oracle.jdbc.OracleTypes;
@WebServlet(name = "DispNotif", urlPatterns = { "/dispnotif" })
public class DispNotif extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=windows-1252";
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html><body>");
ResultSet rs = null;
Connection conn =null;
try {
conn = ConnectionUtils.getConnection();
Class.forName("oracle.jdbc.driver.OracleDriver");


String strQry="{call getUserDetails(?,?)}";
CallableStatement cs = conn.prepareCall(strQry);

cs.setString(1,"CHRISTIAN.MEDINA");
cs.registerOutParameter(2, OracleTypes.CURSOR);
cs.execute();
// get cursor and cast it to ResultSet
rs = (ResultSet) cs.getObject(2);

out.println("<table border=1 width=50% height=30% align=center>");
out.println("<tr><th>NOTIFICATION_ID</th><th>SUBJECT</th><th>AMOUNT</th><th>CURRENCY</th><tr>");


// loop it like normal
while (rs.next()) {
String NotifID = rs.getString("NOTIFICATION_ID");
String Subject = rs.getString("SUBJECT");
String Amount = rs.getString("AMOUNT");
String Currency = rs.getString("CURRENCY");
out.println("<tr><td>" + NotifID + "</td><td>" + Subject + "</td><td>" + Amount + "</td><td>" + Currency + "</td></tr>");
}

out.println("</table>");
out.println("</html></body>");
conn.close();
}
catch (Exception e) {
e.printStackTrace();
out.println("error");
}
}
}

最佳答案

只需使用 request.getParameter("userName") 检索参数的值:

cs.setString(1,request.getParameter("userName"));

关于java - 如何使用 Java servlet 获取用户的动态输入而无需表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54860869/

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