iT邦幫忙

0

找不到錯誤一直出現404

  • 分享至 

  • xImage

https://ithelp.ithome.com.tw/upload/images/20240524/20167283EyNgy6mUfA.png```
WebServlet("/RegisterServlet")

public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
Connection conn;

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    ResultSet rs = null;
    PreparedStatement stmt = null;    
    
    try {
   	  Context context = new InitialContext();
         DataSource ds =(DataSource)context
       		  .lookup("java:/comp/env/jdbc/servdb");
         conn = ds.getConnection();
        String sql = "SELECT * FROM users";
        stmt = conn.prepareStatement(sql);
        rs = stmt.executeQuery();     
        List<LogBean> logs = new ArrayList<>();
        LogBean log = null;
       while (rs.next()) {
    	   log= new LogBean();
    	   log.setId(rs.getInt("Id"));
    	   log.setEmail(rs.getString("Email"));
    	   log.setPassword(rs.getString("Password"));
    	   log.setUsername(rs.getString("Username"));
    	 
    	   logs.add(log);
        }

        request.setAttribute("logs", logs);
        request.getRequestDispatcher("/jsp/register.jsp").forward(request, response);
    } catch (Exception e) {
        throw new ServletException(e);
    } finally {
        try {
            if (rs != null) rs.close();
            if (stmt != null) stmt.close();
            if (conn != null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    doGet(request, response);
}

}






<%@ page language="java" contentType="text/html; charset=UTF-8"
import="java.util.*,com.lcpan.bean.LogBean"%>
<%!@SuppressWarnings("unchecked")%>

	<table border="1">
		<tr style="background-color: #a8fefa">
			<th>
			<th>姓名
			<th>使用者名稱
			<th>密碼
			<th>郵件
			 <%
			List<LogBean> logs = (ArrayList<LogBean>) request.getAttribute("logs");
			for (LogBean log : logs) {
			%>
		<tr>
			<td><%=log.getId()%>
			<td><%=log.getUsername()%>
			<td><%=log.getEmail()%>
			<td><%=log.getPassword()%> <%

}
%>


共<%=logs.size()%>筆會員資料

package com.lcpan.bean;

public class LogBean implements java.io.Serializable {
	private static final long serialVersionUID = 1L;
 

	  private int id;
	    private String username;
	    private String password;
	    private String email;

	    // Getters and setters
	    public int getId() { return id; }
	    public void setId(int id) { this.id = id; }
	    public String getUsername() { return username; }
	    public void setUsername(String username) { this.username = username; }
	    public String getPassword() { return password; }
	    public void setPassword(String password) { this.password = password; }
	    public String getEmail() { return email; }
	    public void setEmail(String email) { this.email = email; }
		@Override
		public String toString() {
			return "LogBean [id=" + id + ", username=" + username + ", password=" + password + ", email=" + email + "]";
		}
	 
	}

一直跑404

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
WUcheap
iT邦研究生 5 級 ‧ 2024-05-24 14:52:24

因為不清楚你的程式架構,但你說出現的404,猜測是相對路徑的問題
request.getRequestDispatcher("/jsp/register.jsp")
改為
request.getRequestDispatcher("register.jsp")

a29037651 iT邦新手 5 級 ‧ 2024-05-24 16:53:22 檢舉

https://ithelp.ithome.com.tw/upload/images/20240524/20167283mxZVR9lkgK.png 改了還是出現404

WUcheap iT邦研究生 5 級 ‧ 2024-05-24 17:27:20 檢舉

你要不要先在doGet裡print看看有沒有進入?
沒有的話,查看根目錄是JSP1?
程式資料夾架構也可以展示一下

a29037651 iT邦新手 5 級 ‧ 2024-05-24 17:49:07 檢舉

https://ithelp.ithome.com.tw/upload/images/20240524/201672832yFW1Sw5KL.png 這裡

我要發表回答

立即登入回答