User616263
User616263

Reputation: 467

How do I execute an SQL SELECT query in JSP?

I want to execute an SQL query in JSP. The display must be in JSP code, not in java.

I cannot introduce JSP code in a java page.

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();


}


} 

Upvotes: 0

Views: 11693

Answers (1)

BalusC
BalusC

Reputation: 1108692

You need to override the real HttpServlet#doPost() method, not to add another one which won't be invoked by the 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>
    <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.

Finally change the HTML form action URL in your JSP so that it matches the servlet URL.

<form action="modification" method="post">

See also:


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.

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.

Upvotes: 4

Related Questions