Reputation: 977
i am having trouble getting the page to work, what i am doing is when i click the link it should connect to the database and display the data to the browser.
this is my jsp code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<%
String str = (String)request.getAttribute("acId");
%>
Account ID: <a href="detailsservlet"> <%= str %>
</body>
</html>
this is my servlet code:
package com.Project.Hdfc;
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.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class detailsservlet extends HttpServlet {
@Override
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
Connection con;
Statement stmt;
ResultSet rs;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:DSN","scott","krishna");
stmt = con.createStatement();
rs = stmt.executeQuery("Select * from custm");
pw.println("Id "+ " Name" +" Address" + "<br>");
while(rs.next())
{
pw.println(rs.getInt(1)+" "+rs.getString(2) + " " + rs.getString(3) + "<br>");
}
}
catch (Exception e){
pw.println(e);
}
}
}
Upvotes: 1
Views: 4500
Reputation: 5300
You have a doPost method but not doGet method in your sevlet code. If you only want to get information from the jsp page, but not to post any data to the database, then only use doGet. If you want to both post and get, a kind of solution is to
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doPost(req, res);
}
or the other way around, as thats nicer.
Also you shouldnt mix html tags in your Java code.
Another solution to your code is to change your a href="detailssevlet" to a input type button inside a element, as that will provide a POST to the sever, and not a GET, if thats what you want to do, but not in this case, since you fetch data, not submit it to the server.
Upvotes: 1
Reputation: 6675
I believe you want your doPost
method to be named doGet
instead.
Upvotes: 0