java 如何在 JSP 中执行 SQL SELECT 查询?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5330046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I execute an SQL SELECT query in JSP?
提问by User616263
I want to execute an SQL query in JSP. The display must be in JSP code, not in java.
我想在 JSP 中执行 SQL 查询。显示必须是 JSP 代码,而不是 java。
I cannot introduce JSP code in a java page.
我无法在 java 页面中引入 JSP 代码。
package tn.com.tradenet.utilisateur;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class Modification extends HttpServlet
{
public void doPost()
{
try
{
String id ="1"; //request.getParameter("userName");
String nom ="mecchlaoui"; //request.getParameter("userName");
String prenom ="fawzia"; //request.getParameter("userName");
String email ="hotmail"; //request.getParameter("password");
String profil ="fawzia"; //request.getParameter("password");
String login ="fawzia"; //request.getParameter("password");
String pass ="1258"; //request.getParameter("password");
ConnectionBD mod = new ConnectionBD();
//String sql="SELECT id FROM utilisateur";
//ResultSet res=mod.execMonSQl(sql);
//while (res.next())
//{
//id = res.getString(1);
//}
mod.execMonUpdate("UPDATE utilisateur SET nom='"+nom+"',prenom='"+prenom+"', email='"+email+"', profil='"+profil+"',login='"+login+"',pass='"+pass+"' WHERE 'id'='"+id+"'");
System.out.println("element ajoutté");}
catch(SQLException s)
{
System.out.println("erreur" +s);
}
}
public static void main(String[] args) {
Modification mdf =new Modification();
mdf.doPost();
}
}
回答by BalusC
You need to override the realHttpServlet#doPost()
method, not to add another one which won't be invoked by the servletcontainer.
您需要覆盖真正的HttpServlet#doPost()
方法,而不是添加另一个不会被 servletcontainer 调用的方法。
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Do your job here.
}
And you need to map this servlet in web.xml
on a known URL pattern.
并且您需要将这个 servlet 映射web.xml
到一个已知的 URL 模式中。
<servlet>
<servlet-name>modification</servlet-name>
<servlet-class>com.example.Modification</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>modification</servlet-name>
<url-pattern>/modification</url-pattern>
</servlet-mapping>
With the above <url-pattern>
the servlet will listen on URL http://example.com/context/modification.
有了上面<url-pattern>
的 servlet 将侦听 URL http://example.com/context/modification。
Finally change the HTML form action URL in your JSP so that it matches the servlet URL.
最后更改 JSP 中的 HTML 表单操作 URL,使其与 servlet URL 匹配。
<form action="modification" method="post">
See also:
也可以看看:
- Servlets tag info page- How servlets work and a little Hello World
- Servlets 标签信息页面- servlets 的工作原理和一点 Hello World
Unrelated to the concrete question/problem, note that you still need to change your servlet code to display some result page in flavor of a JSP. E.g.
与具体问题/问题无关,请注意,您仍然需要更改 servlet 代码以显示一些具有 JSP 风格的结果页面。例如
request.getRequestDispatcher("/WEB-INF/result.jsp").forward(request, response);
Also, the main()
method inside the servlet makes no sense, remove it. Last but not least, your SQL approach is sensitive to SQL injection attacks. Learn PreparedStatement
.
另外,main()
servlet 内部的方法没有意义,将其删除。最后但并非最不重要的一点是,您的 SQL 方法对SQL 注入攻击很敏感。学习PreparedStatement
。