Java 表单加载,但进程不会停止
从JSP页面中的arraylist查看记录时遇到问题。每次我通过javascript onload事件自动加载JSP页面时,都会显示数据,但过程不会停止Java 表单加载,但进程不会停止,java,javascript,jsp,servlets,Java,Javascript,Jsp,Servlets,从JSP页面中的arraylist查看记录时遇到问题。每次我通过javascript onload事件自动加载JSP页面时,都会显示数据,但过程不会停止 Categoriaclass: package proyecto.modelo; public class Categoria { private int idcategoria; public int getIdcategoria() { return idcategoria; } pu
Categoria
class:
package proyecto.modelo;
public class Categoria {
private int idcategoria;
public int getIdcategoria() {
return idcategoria;
}
public void setIdcategoria(int idcategoria) {
this.idcategoria = idcategoria;
}
}
package proyecto.dao;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDAO {
protected void cerrarConexion(Connection con) throws RuntimeException {
try {
if (con != null && !con.isClosed()) {
con.close();
}
} catch (SQLException se) {
System.err.println("Error: cerrarConexion: " + se);
}
}
protected void cerrarResultSet(ResultSet rs) throws RuntimeException {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException se) {
System.err.println("Error: cerrarResultSet: " + se);
}
}
protected void cerrarStatement(PreparedStatement stmt)
throws RuntimeException {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException se) {
System.err.println("Error: cerrarStatement: " + se);
}
}
protected void cerrarCallable(CallableStatement callstmt)
throws RuntimeException {
try {
if (callstmt != null) {
callstmt.close();
}
} catch (SQLException se) {
System.err.println("Error: cerrarCallable: " + se);
}
}
}
package proyecto.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import proyecto.excepcion.DAOExcepcion;
import proyecto.modelo.Categoria;
import proyecto.util.ConexionBD;
public class CategoriaDAO extends BaseDAO {
public Collection<Categoria> listarIdCat() throws DAOExcepcion{
Collection<Categoria> = new ArrayList<Categoria>();
String query = "SELECT id_categoria from categoria ";
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con=ConexionBD.obtenerConexionDirecta();
stmt=con.prepareStatement(query);
rs=stmt.executeQuery();
while(rs.next()) {
Categoria vo=new Categoria();
vo.setIdcategoria(rs.getInt("id_categoria"));
c.add(vo);
}
} catch (SQLException e) {
System.err.println(e.getMessage());
throw new DAOExcepcion(e.getMessage());
} finally {
this.cerrarStatement(stmt);
this.cerrarResultSet(rs);
this.cerrarConexion(con);
}
return c;
}
}
package proyecto.negocio;
import java.util.Collection;
import java.util.List;
import proyecto.dao.CategoriaDAO;
import proyecto.excepcion.DAOExcepcion;
import proyecto.modelo.Categoria;
public class CategoriaNegocio {
public Collection<Categoria> listarIdCat() throws DAOExcepcion {
CategoriaDAO dao = new CategoriaDAO();
Collection<Categoria> lista = dao.listarIdCat();
return lista;
}
}
BaseDAO
class:
package proyecto.modelo;
public class Categoria {
private int idcategoria;
public int getIdcategoria() {
return idcategoria;
}
public void setIdcategoria(int idcategoria) {
this.idcategoria = idcategoria;
}
}
package proyecto.dao;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDAO {
protected void cerrarConexion(Connection con) throws RuntimeException {
try {
if (con != null && !con.isClosed()) {
con.close();
}
} catch (SQLException se) {
System.err.println("Error: cerrarConexion: " + se);
}
}
protected void cerrarResultSet(ResultSet rs) throws RuntimeException {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException se) {
System.err.println("Error: cerrarResultSet: " + se);
}
}
protected void cerrarStatement(PreparedStatement stmt)
throws RuntimeException {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException se) {
System.err.println("Error: cerrarStatement: " + se);
}
}
protected void cerrarCallable(CallableStatement callstmt)
throws RuntimeException {
try {
if (callstmt != null) {
callstmt.close();
}
} catch (SQLException se) {
System.err.println("Error: cerrarCallable: " + se);
}
}
}
package proyecto.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import proyecto.excepcion.DAOExcepcion;
import proyecto.modelo.Categoria;
import proyecto.util.ConexionBD;
public class CategoriaDAO extends BaseDAO {
public Collection<Categoria> listarIdCat() throws DAOExcepcion{
Collection<Categoria> = new ArrayList<Categoria>();
String query = "SELECT id_categoria from categoria ";
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con=ConexionBD.obtenerConexionDirecta();
stmt=con.prepareStatement(query);
rs=stmt.executeQuery();
while(rs.next()) {
Categoria vo=new Categoria();
vo.setIdcategoria(rs.getInt("id_categoria"));
c.add(vo);
}
} catch (SQLException e) {
System.err.println(e.getMessage());
throw new DAOExcepcion(e.getMessage());
} finally {
this.cerrarStatement(stmt);
this.cerrarResultSet(rs);
this.cerrarConexion(con);
}
return c;
}
}
package proyecto.negocio;
import java.util.Collection;
import java.util.List;
import proyecto.dao.CategoriaDAO;
import proyecto.excepcion.DAOExcepcion;
import proyecto.modelo.Categoria;
public class CategoriaNegocio {
public Collection<Categoria> listarIdCat() throws DAOExcepcion {
CategoriaDAO dao = new CategoriaDAO();
Collection<Categoria> lista = dao.listarIdCat();
return lista;
}
}
CategoriaDAO
class:
package proyecto.modelo;
public class Categoria {
private int idcategoria;
public int getIdcategoria() {
return idcategoria;
}
public void setIdcategoria(int idcategoria) {
this.idcategoria = idcategoria;
}
}
package proyecto.dao;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDAO {
protected void cerrarConexion(Connection con) throws RuntimeException {
try {
if (con != null && !con.isClosed()) {
con.close();
}
} catch (SQLException se) {
System.err.println("Error: cerrarConexion: " + se);
}
}
protected void cerrarResultSet(ResultSet rs) throws RuntimeException {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException se) {
System.err.println("Error: cerrarResultSet: " + se);
}
}
protected void cerrarStatement(PreparedStatement stmt)
throws RuntimeException {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException se) {
System.err.println("Error: cerrarStatement: " + se);
}
}
protected void cerrarCallable(CallableStatement callstmt)
throws RuntimeException {
try {
if (callstmt != null) {
callstmt.close();
}
} catch (SQLException se) {
System.err.println("Error: cerrarCallable: " + se);
}
}
}
package proyecto.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import proyecto.excepcion.DAOExcepcion;
import proyecto.modelo.Categoria;
import proyecto.util.ConexionBD;
public class CategoriaDAO extends BaseDAO {
public Collection<Categoria> listarIdCat() throws DAOExcepcion{
Collection<Categoria> = new ArrayList<Categoria>();
String query = "SELECT id_categoria from categoria ";
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con=ConexionBD.obtenerConexionDirecta();
stmt=con.prepareStatement(query);
rs=stmt.executeQuery();
while(rs.next()) {
Categoria vo=new Categoria();
vo.setIdcategoria(rs.getInt("id_categoria"));
c.add(vo);
}
} catch (SQLException e) {
System.err.println(e.getMessage());
throw new DAOExcepcion(e.getMessage());
} finally {
this.cerrarStatement(stmt);
this.cerrarResultSet(rs);
this.cerrarConexion(con);
}
return c;
}
}
package proyecto.negocio;
import java.util.Collection;
import java.util.List;
import proyecto.dao.CategoriaDAO;
import proyecto.excepcion.DAOExcepcion;
import proyecto.modelo.Categoria;
public class CategoriaNegocio {
public Collection<Categoria> listarIdCat() throws DAOExcepcion {
CategoriaDAO dao = new CategoriaDAO();
Collection<Categoria> lista = dao.listarIdCat();
return lista;
}
}
ServletsdoPost()
方法:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
CategoriaNegocio negocio = new CategoriaNegocio();
try {
Collection<Categoria> lista = negocio.listarIdCat();
request.setAttribute("IDCATEGORIA", lista);
} catch (DAOExcepcion e) {
System.out.println(e.getMessage());
}
RequestDispatcher rd = request.getRequestDispatcher("listar_idcat.jsp");
rd.forward(request, response);
}
protectedvoiddopost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
Category Anegocio negocio=新的Category Anegocio();
试一试{
集合lista=negocio.listarIdCat();
setAttribute(“IDCATEGORIA”,lista);
}渔获物(DAOExcepcion e){
System.out.println(e.getMessage());
}
RequestDispatcher rd=request.getRequestDispatcher(“listar_idcat.jsp”);
转发(请求、响应);
}
JSP:
window.onload=函数(){
document.formulario.submit()
};
document.close();
${c.idcategoria}
您似乎希望在GET请求中调用servlet。你走错路了。您不应该在加载页面时提交帖子表单。您应该在servlet的doGet()
方法中执行作业,并直接调用它
您需要进行以下更改:
/listar\u idcat
doPost
方法重命名为doGet
listar\u idcat.jsp
文件移动到/WEB-INF
文件夹中(这样可以防止最终用户直接访问)
getRequestDispatcher(“listar_idcat.jsp”)代码>在servlet中调用getRequestDispatcher(“/WEB-INF/listar_idcat.jsp”)代码>
http://localhost:8080/context/listar_idcat
(是的,没有.jsp
扩展!它将直接调用servlet的doGet()
)
另见:
- -你好,世界2号
字符,这使得代码在语法上无效。你不应该那样做。只需选择代码并按下代码按钮。这就是全部。真的。非常感谢你,朋友。最近我开始了网络java的世界,如果你能帮助我了解一些手册,朋友,谢谢。你好,巴卢斯,如果你能在组合框插入数据后给我一个listarme的想法。如果你有一个新的独立问题,你应该按问问题按钮。您不应在完全不同的问题上使用添加注释
按钮。