• 締切済み

forward()でエラーが出て対応ができません。

どなたか、教えてください。 サーブレットからDBに接続して、検索条件(名前)が一致しているレコードを取得。 request.setAttribute リクエストデータにその人の住所をセットし、別のjspファイルに送り 表示するプログラムなんですが。 getServletConfig().getServletContext().getRequestDispatcher("/jsp/result1.jsp").forward(request, response);の部分で、コミット済みというエラーが発生します。 下記がそのプログラムです。 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws     ServletException, IOException {      request.setCharacterEncoding("windows-31J");      String name = request.getParameter("name1");      Connection con=null;;      PreparedStatement ps =null;  try{       Context context = new InitialContext();     DataSource ds =(DataSource)context.lookup("java:comp/env/jdbc/FrameWork");     con = ds.getConnection();     ps = con.prepareStatement("SELECT * FROM address where name like ?;");     ps.setString(1, name);     ResultSet rs = ps.executeQuery();    rs.next();    String address =rs.getString("address");   request.setAttribute("address1", address);    rs.close();    ps.close(); }catch(Exception e){ e.printStackTrace(); }finally{ try{ con.close();             }catch(SQLException e){e.printStackTrace();} }     try{ getServletConfig().getServletContext().getRequestDispatcher ( "/jsp/result1.jsp").forward(request, response);       }catch(Exception e){System.out.print(e);} } } エラーにも関わらず、ページは切り替わるのですが、アドレスはNULL値が表示されます。 一応、デバックをしてSQLの取得、リクエストデータのセットまでは、問題ないのは確認済みです。 乱文でわかりづらいのですが、どなたかお願いいたします。

  • Java
  • 回答数1
  • ありがとう数0

みんなの回答

  • x_jouet_x
  • ベストアンサー率68% (162/236)
回答No.1

try { ... } catch(Exception e) { System.out.print(e); } としているのであれば、実行環境の標準出力かログファイルにエラーログが出力されていると思います。 それを教えて下さい。

関連するQ&A

  • ラジオボタンを使って更新 eclipse Tomcat mysql

    ラジオボタンを使って更新するときにデータがnullになってしまう。 更新するときデータが表示状態でデータを買えて更新したい kousin1.java import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class kousin1 extends HttpServlet{ public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ doPost(request,response); } public void doPost PrintWriter out = response.getWriter(); String url = "jdbc:mysql://localhost:3306/aiue"; String user = "root"; //ユーザ名 String pass =""; //パスワード String id1 =""; String name1=""; String address1=""; Connection con = null; //データベースへの接続を行なう Statement smt = null; //SQL実行の仲介者 int syutoku = 0; ResultSet rs = null; String id = request.getParameter("id"); String name = request.getParameter("name"); String address = request.getParameter("address"); String radio = request.getParameter("radio"); try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); con = DriverManager.getConnection(url,user,pass); smt = con.createStatement(); System.out.println("更新"); String sql1 = "UPDATE System.out.println(sql1); smt.executeUpdate(sql1); smt.close(); }catch(SQLException e){ throw new ServletException(e); }catch(Exception e){ e.printStackTrace(); }finally{ if(smt != null){ try{smt.close();}catch(SQLException ignore){} } if(con != null){ try{con.close();}catch(SQLException ignore){} } kousin1.jspへ kousin.jsp <html> <head> </head> <form method="POST" action="kousin1"> <body> <div>コード:<input type="text" name="id" value="<%=request.getAttribute("id")%>" size=40></div> <div>名称:<input type="text" name="name" value="<%=request.getAttribute("name")%>" size=42></div> <div>住所:<input type="text" name="address" value="<%=request.getAttribute("address")%>" size=42></div> <div><input type="hidden" name="syutoku" value="<%=request.getAttribute("syutoku")%>" size=40></div> <input type="submit" value="更新"> </form> </body> </html>

  • webアプリを作ってます。

    webアプリを作ってます。 sqlの箇所で型の不一致stringからstringは変換できませんと 出てきます。 調べてもわかりませんでした。 どこが間違ってるのか教えてください。 package info.searchman; import 省略 public class Dbinsert extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; private ServletContext context; //private Connection connection; public void init(ServletConfig config) throws ServletException { context=config.getServletContext();} public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=Shift_JIS"); PrintWriter out=response.getWriter(); out.println("<html><head><title>Sample199</title></head><body>"); try { int type=0; String name=null; int price=0; String html=null; //Connection con = null; type=Integer.parseInt(request.getParameter("type")); //name= request.getParameterValues("name"); //String name = request.getParameter("name"); name = redecode(request.getParameter("name")); price=Integer.parseInt(request.getParameter("price")); Class.forName("org.postgresql.Driver"); Connection con = DriverManager.getConnection("jdbc:postgresql:postgres","postgres","gokui"); // ステートメントオブジェクトを生成 Statement stmt = con.createStatement(); String values2 = "(\'"+type+"\', \'"+name+"\', \'"+price+"\')"; String sql="INSERT INTO commodity values " + values2 ; stmt.executeUpdate(sql); //ResultSet rs = stmt.executeQuery(sql); out.println("データを登録しました。"); //out.println("</table>"); //rs.close(); stmt.close(); con.close(); } catch(Exception e) { context.log("denaositekoi"); e.printStackTrace(out); } out.println("</body></html>"); out.close(); } private String redecode(java.lang.String parameter) { // TODO 自動生成されたメソッド・スタブ return null; } }

    • ベストアンサー
    • Java
  • Javaです。

    以下のSQLのcustomerのc_idの最大値を求め、表示させたいのですが、上手くいかず、エラーが返ります。 Customerはデータベースに格納されています。 以下ソースです。 ・・・・・・・・・ String sql="select max(c_id) as max_c from customer"; Connection con = null; Statement smt = null; try { con = DBManager.getConnection(); smt = con.createStatement(); ResultSet rs = smt.executeQuery(sql); out.println(rs.getInt("max_c")); }catch(SQLException e){ throw new ServletException(e); }finally{ if(smt != null){ try{smt.close();}catch(SQLException ignore){} } if(con != null){ try{con.close();}catch(SQLException ignore){} } } ・・・・・・・ //恐らく、out.println(rs.getInt("max_c")); の表示クエリに問題があると思うのですが、どうすればいいか分からず質問しました。 具体的なソース等、解決策を教えて下さい。 Java素人です。 よろしくお願いします。

    • ベストアンサー
    • Java
  • ファイルダウンロード後のフォワードについて

    サーブレット内で、ファイルのダウンロードをした後に処理をJSPへフォワードしたいのですが、現在、ダウンロードしたあとフォワードしてもJSPが機能しなくて困りました。 コードとしては以下の通りです(一部抜粋ですみません) /* ファイルダウンロードの設定 */ response.setContentType("application/octet-stream; charset=Shift-JIS"); response.setHeader("Content-disposition","attachement; filename= "+sgy_y+sgy_m+"_"+simekbn+".csv"); PrintWriter pw = response.getWriter(); /* CSVファイル出力 */ pw.println("あああ,いいい,ううう,えええ,おおお"); pw.println("かかか,ききき,くくく,けけけ,こここ"); pw.println("さささ,ししし,すすす,せせせ,そそそ"); pw.close(); /* JSPへフォワード */ getServletConfig().getServletContext().getRequestDispatcher("/xxx.jsp").forward(request,response); 何か良いアドバイスがありましたら、宜しくお願いします。

    • ベストアンサー
    • Java
  • jsp、サーブレットの質問です。

    Tomcatを使用してjspとサーブレットを使い掲示板のような投稿サイトもどきを作成していますが解決できない部分があります。 ↓は表示用のjspです♪ <html> <head> <title>Insert title here</title> </head> <body> <% String lsts = (String)request.getAttribute("lst"); %> <table border="4"> <tr> <td>日時</td> <td>タイトル</td> <td>スレッド主</td> <td></td> </tr> <tr> <td><%for(int i = 0; i<lsts.length(); i++){ out.println(lsts); } %> </td> <td><%out.println(); %></td> <td><%out.println("");%></td> <td> </td> </tr> </table> </form> </body> </html> こちらがサーブレットです otected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /*DB接続処理は省略します*/ try{ ps = (PreparedStatement) con.prepareStatement(sql); String sql = "select * from thred"; ResultSet rs = ps.executeQuery(); //テーブル内一覧取得 while(rs.next()){ rs.getString("time"); rs.getString("title"); rs.getString("id"); rs.getString("thredid"); String s = (rs.getString("time")+","+ rs.getString("title")+","+rs.getString("id")); String[] lsts = s.split(","); List<String> thlists = Arrays.asList(lsts); String ss = (thlists.get(0)); request.setAttribute("thlst",ss); RequestDispatcher dispatcher= request.getRequestDispatcher("/list.jsp"); dispatcher.forward(request,response); } }catch(SQLException e){ e.printStackTrace(); } catch(ClassNotFoundException e){ e.printStackTrace(); }finally{ try { con.close(); } } } jspの方にサーブレットでthredDBから抜き取ったtimeとtitleとidを全て表示させたいのですが 現状だと 2014-06-03 06:45:02  aaaa 1 の行だけがたくさん表示されてしまいます 恐らくスコープを取得した時点でこの↑の1行のものしかとれてこれていないのだと思います... どのように処理を行ったらよいのでしょうか? DBの中身は画像添付いたしますっ

  • getRequestDispatcher の機能について

    こんいちは。 下記の機能を使用して転送したいのですがエラーになってしまいます。 tomcatの設定ファイル、web.xmlなどのファイルに何か情報を登録しなければならないのでしょうか? どなたかご存知でしたら教えていただけないでしょうか? 環境 tomcat4.1 機能 ****** getServletConfig().getServletContext().getRequestDispatcher("/cc/t_1.jsp").forward(request,response); ****** ソース package cc; import mng.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class t_1 extends HttpServlet { public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { //response.sendRedirect("/cc/t_1.jsp"); getServletConfig().getServletContext().getRequestDispatcher("/cc/t_1.jsp").forward(request,response); } public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { doGet(request,response); } } 下記がエラー内容になります。 HTTP Status 404 - /cc/t_1.jsp The requested resource (/cc/t_1.jsp) is not available.

    • ベストアンサー
    • Java
  • doPost+doGet

    public class Class{ public void doPost (HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException try{ //処理 }catch(Exception e){ e.printStackTrace(); } } public void doGet (HttpServletRequest request,HttpServletResponse response){ try{ doPost(request, response); }catch(Exception e){ e.printStackTrace(); } } } この処理は、doGetで送信されたものをdoPostに送って処理するということでよろしいのでしょうか? 分かる方がいましたら、よろしくお願いします。

    • ベストアンサー
    • Java
  • requestについて困っています。

    下記のbooleanの結果を他のサーブレットで取得して、結果ごとに"mapping.findForward("")"の処理をしたいのですが、取得の仕方が分かりません。お願い致します。 <MemberDAO.java> ・・・ public boolean isEntriedMail(String member_mail) throws SQLException{ Connection con = ds.getConnection(); PreparedStatement stmt = con.prepareStatement(ISENTRIEDSQL); ResultSet rs = null; try{ stmt.setString(1,member_mail); rs = stmt.executeQuery(); if(rs.next()){ int count = rs.getInt(1); if(count == 0){ return false; } } }catch(SQLException ex){ return true; }finally{ if(rs != null){ rs.close(); } stmt.close(); con.close(); } return true; }

  • DAOクラスの使い方

    ある書籍を参考に勉強しております。以下のDAOクラスのデータ追加に関して使い方がわかりません。 サーブレット、もしくはJSPにどのように書けば、DBにデータを追加できるようになるのでしょうか? 宜しく御願い致します。 package chap11_database.sec03; import java.sql.*; import java.util.ArrayList; public class EmployeeDAO { //データベースとの接続に必要な情報を保持するフィールド変数 private final static String DRIVER_URL ="jdbc:mysql://192.168.2.200:3306/satoh" + "?useUnicode=true&characterEncoding=utf8"; private final static String DRIVER_NAME = "com.mysql.jdbc.Driver"; private final static String USER_NAME = "root"; private final static String PASSWORD = "1qaz!QAZ"; //接続メソッド protected Connection createConnection(){ try{ Class.forName(DRIVER_NAME); Connection con = DriverManager.getConnection(DRIVER_URL, USER_NAME, PASSWORD); return con; } catch(Exception e){ e.printStackTrace(); } return null; //例外が発生した場合はnullを返す。 } //切断メソッド protected void closeConnection(Connection con){ try{ con.close(); } catch(Exception ex){ ex.printStackTrace(); } } public ArrayList<Employee> findAll(){ //社員一覧を保持するリスト ArrayList<Employee> list = new ArrayList<Employee>(); Connection con = null; try{ //データベースに接続 con = createConnection(); //データベースにSQL文を送信 String sql = "select * from employee"; Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(sql); while(rs.next()){ int id = rs.getInt("id"); String name = rs.getString("name"); String address = rs.getString("address"); int tel = rs.getInt("tel"); Date birthday = rs.getDate("birthday"); Employee employee = new Employee(id, name, address, tel, birthday); //TO(Employeeオブジェクト)を、社員一覧を保持するリス手に追加 list.add(employee); } //社員一覧を返す。 return list; }catch(Exception e){ e.printStackTrace(); return null; }finally{ //データベースからの切断 closeConnection(con); } } //社員登録メソッド public void create(Employee emp){ Connection con = null; try{ //データベースに接続 con = createConnection(); //データベースにSQL文を送信 String sql = "insert into employee values(?, ?, ?, ? ,?)"; PreparedStatement stmt = con.prepareStatement(sql); stmt.setInt(1, emp.getId()); stmt.setString(2, emp.getName()); stmt.setString(3, emp.getAddress()); stmt.setInt(4, emp.getTel()); stmt.setDate(5, emp.getBirthday()); stmt.executeUpdate(); }catch(Exception e){ e.printStackTrace(); }finally{ //データベースからの切断 closeConnection(con); } } }

    • ベストアンサー
    • Java
  • RequestDispatcher でURL引数の扱い

    はじめまして。SINといいます。 今、ServletとJSPを使っているんですが、JSPからServletにはURL引数を渡す ことはできているんですが、ServletからJSPにはURL引数を渡すことはできるのでしょうか? 色々と調べていたんですが、わからず困っています。 SrvletからJSPへはRequestDispatcher の forward メソッドを使っています。 どなたか知恵をお貸しください。 下記にソースを抜粋したものを書きました。 String nextUrl = "/xxxx/xxxxx.jsp?aaa"; ServletContext context = request.getSession().getServletContext(); RequestDispatcher requestDispatcher = context.getRequestDispatcher(nextUrl); requestDispatcher.forward(request, response);

専門家に質問してみよう