If I run the below servlet, it runs, but if I click on the onclick mytest part
nothing happens.
However if I load this same servlet with no changes into a div on an html page or a jsp page, then clicking on the
onclick mytest part works.
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.lang.String.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author jimwin7a
*/
public class petlist extends HttpServlet {
Connection theConnection;
private ServletConfig config;
@Override
public void init(ServletConfig config)
throws ServletException{
this.config=config;
}
@Override
public void service (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
HttpSession session = req.getSession(true);
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><HEAD>");
out.println("<script language=\\"text/javascript\\" src=\\"include/jquery.js\\"></script>");
out.println("<script language=\\"text/javascript\\" src=\\"include/myjq.js\\"></script>");
out.println("</HEAD>");
out.println("<BODY bgColor=blanchedalmond text=#008000 topMargin=0>");
out.println("<P align=center>");
out.println("<form name=\\"petsedit\\" id=\\"petsedit\\" action=\\"pagetest.jsp\\" method=\\"POST\\">");
out.println("<TABLE id=\\"myTable\\" align=center border=1 cellPadding=2 cellSpacing=2>");
try{
Class.forName("com.mysql.jdbc.Driver");
theConnection = DriverManager.getConnection("jdbc:mysql:///petback2", "root", "sa");
SQLBean.DbBean db = new SQLBean.DbBean();
String spetid;
spetid = req.getParameter("petid");
out.println(req.getParameter("petid"));
String t11;
t11 = db.convJS(t1);
String query = "";
query = "select * from pets WHERE petid = '" + spetid + "'";
Statement theStatement=theConnection.createStatement();
ResultSet rs=theStatement.executeQuery(query); //Select all records from emaillists table.
while (rs.next())
{
out.println("<tr><td>Pet name</td>");
out.println("<td><input type=\\"Text\\" name=\\"petname\\" id=\\"petname\\" size=\\"20\\" value='" + rs.getString("petname") + "'/></td></tr>");
out.println("<tr><td>species</td>");
out.println("<td><input type=\\"Text\\" name=\\"species\\" id=\\"species\\" size=\\"20\\" value='" + rs.getString("species") + "'/></td></tr>");
}
out.println("</TABLE></P>");
out.println("<a href="+session.getAttribute( "sessvar" )+">sessback</a>");
out.println("<input type=\\"hidden\\" name=\\"petid\\" id=\\"petid\\" value="+spetid+"/>");
out.println("<form>");
out.println("</ br>");
out.println("<a href=\\"#\\" onclick=\\"mytest('"+spetid+"')\\" >try this</a>");
rs.close();//Close the result set
theStatement.close();//Close statement
theConnection.close(); //Close database Connection
}catch(Exception e){
out.println(e.getMessage());//Print trapped error.
}
out.println("<P> </P></FONT></BODY></HTML>");
}
@Override
public void destroy(){
}
}
Code for the function:
function mytest(petid)
{
alert("hello..."+petid);
}
Can someone tell me why a servlet is like this? Can run a jquery/javascript
function if servlet is loaded in a div. Cannot run a jquery/javascript if servlet is loaded alone.