0

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


}


} 
2
  • 2
    18 questions with 0 accepted answers is... well, unacceptable. Commented Mar 16, 2011 at 18:37
  • @StriplingWarrior, be fair only 12 are eligible :P Commented Mar 16, 2011 at 18:41

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+1. Excellent answer on a difficult question. You might also point him to a parameterized query reference, so he's not open to SQL injection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.