2017-06-09 107 views
0

我已經創建了一個使用(jsp,servlet,apache tomcat 9,eclipse neon,oracle 11g)的servlet項目。我創建了index.jsp(爲登錄細節並將其重定向到LoginServlet,以便在數據庫表中進行用戶名和密碼驗證。在index.jsp中,我甚至提到了一個「register.jsp」鏈接,用於在第一次用戶時註冊)。當我運行項目index.jsp頁面打開。HTTP狀態[404]? [未找到](原始服務器沒有找到當前代表)

當我提供用戶ID和密碼的詳細信息(在數據庫中手動輸入用戶名和密碼的詳細信息)時,它已成功將我的LoginServlet重定向到servlet。

但是,當我點擊「註冊」鏈接,我得到上述錯誤(HTTP狀態[404]?[未找到]類型狀態報告消息/TodayServlet/register.jsp描述原始服務器沒有找到目標資源的當前代表或不願意透露存在的問題Apache Tomcat/9.0.0.M20

我的問題是從index.jsp,我無法打開「register.jsp」 (「通過index.jsp中的錨標記提供的register.jsp)

請查找我的附件以瞭解目錄結構和主jsp和servlets以及web.xml代碼。

的index.jsp(主代碼):

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
<center> 
<form action="LoginServlet" method="post"> 
enter name:<input type="text" name="name"><br> 
enter pass:<input type="password" name="pass"><br> 

<a href="register.jsp">New User Register here...!!!</a><br> 
<input type="submit" value="login"><br> 
</form> 
</center> 
</body> 
</html> 

register.jsp(主代碼):

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
<center> 
<form action="RegisterServlet" method="post"> 
enter name:<input type="text" name= "name1"> <br> 
enter pass:<input type="text" name= "pass1"><br> 
enter email:<input type="text" name="email1"><br> 
<input type="submit" value="register"><br> 
</form> 
</center> 
</body> 
</html> 

RegisterServlet.java(主代碼):

package com.pack; 

import java.io.IOException; 
import java.io.PrintWriter; 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 


public class RegisterServlet extends HttpServlet { 

    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { 
     res.setContentType("text/html"); 
     PrintWriter out=res.getWriter(); 
     String uname1=req.getParameter("name1"); 
     String upass1=req.getParameter("pass1"); 
     String uemail1=req.getParameter("email1"); 
     req.setAttribute("username", uname1); 
     try{ 
     Connection conn=DbConnection.getConnection(); 
     PreparedStatement ps=conn.prepareStatement("insert into register_table values(?,?,?)"); 
     ps.setString(1, uname1); 
     ps.setString(2, upass1); 
     ps.setString(3, uemail1); 
     int i=ps.executeUpdate(); 
     if(i>0){ 
      req.getRequestDispatcher("WelcomeServlet").forward(req, res); 
     } 
     else{ 
      out.print("try registering again "); 
      req.getRequestDispatcher("register.jsp").include(req, res); 
     } 
     } 
      catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 

} 

我與其他servlet沒有問題(LoginServlet,WelcomeServlet ...)

LoginServlet.java(整個代碼):

package com.pack; 

import java.io.IOException; 
import java.io.PrintWriter; 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 

import javax.servlet.RequestDispatcher; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

public class LoginServlet extends HttpServlet { 

    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { 
    RequestDispatcher rd= null; 
     res.setContentType("text/html"); 
    PrintWriter out=res.getWriter(); 
    String upassword=req.getParameter("pass"); 
    out.println(upassword); 

    String uname2=req.getParameter("name"); 
    out.println(uname2); 
    try{ 
     Connection con=DbConnection.getConnection(); 
     out.println("conection established"); 
     PreparedStatement ps=con.prepareStatement("select * from register_table where username=? and password=?"); 
     ps.setString(1, uname2); 
     ps.setString(2, upassword); 
     ResultSet rs= ps.executeQuery(); 

     if(rs.next()){ 
      out.println("username is "+rs.getString(1)+" and pwd is "+rs.getString(2)); 
      req.setAttribute("uid", uname2); 
      req.getRequestDispatcher("MainServlet").forward(req, res); 
     }else{ 
      out.println("user name or password incorrect.Check and login again or register"); 
      req.getRequestDispatcher("index.jsp").include(req, res); 
     } 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 

    } 

} 

web.xml文件(代碼):

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 
    <display-name>TodayServlet</display-name> 
    <welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
    <welcome-file>index.htm</welcome-file> 
    <welcome-file>index.jsp</welcome-file> 
    <welcome-file>default.html</welcome-file> 
    <welcome-file>default.htm</welcome-file> 
    <welcome-file>default.jsp</welcome-file> 
    </welcome-file-list> 
    <servlet> 
    <description></description> 
    <display-name>LoginServlet</display-name> 
    <servlet-name>LoginServlet</servlet-name> 
    <servlet-class>com.pack.LoginServlet</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>LoginServlet</servlet-name> 
    <url-pattern>/LoginServlet</url-pattern> 
    </servlet-mapping> 
    <servlet> 
    <description></description> 
    <display-name>RegisterServlet</display-name> 
    <servlet-name>RegisterServlet</servlet-name> 
    <servlet-class>com.pack.RegisterServlet</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>RegisterServlet</servlet-name> 
    <url-pattern>/RegisterServlet</url-pattern> 
    </servlet-mapping> 
    <servlet> 
    <description></description> 
    <display-name>WelcomeServlet</display-name> 
    <servlet-name>WelcomeServlet</servlet-name> 
    <servlet-class>com.pack.WelcomeServlet</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>WelcomeServlet</servlet-name> 
    <url-pattern>/WelcomeServlet</url-pattern> 
    </servlet-mapping> 
    <servlet> 
    <description></description> 
    <display-name>MainServlet</display-name> 
    <servlet-name>MainServlet</servlet-name> 
    <servlet-class>com.pack.MainServlet</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>MainServlet</servlet-name> 
    <url-pattern>/MainServlet</url-pattern> 
    </servlet-mapping> 

</web-app> 

目錄結構:

  1. TodayServlet(項目名稱)>來源>融爲一體。 pack> servlets
  2. web-inf>(web.xml和jsp文件)

directory structure

+0

404簡單地表示文件不可用,或者提到的路徑不正確。對於JSP,目錄應該是這樣的:Project-> WebContent-> WEB-INF-> jsps –

+0

遷移Pigeon,請檢查我提供的代碼,我創建了所有必需的jsps.i創建了index.jsp和register.jsp(但無法從index.jsp訪問register.jsp)它唯一的問題,這裏它的拋出錯誤[404原始服務器沒有找到目標資源的當前表示,或者不願意透露存在。] – jahnavi

+0

嘗試此操作,轉至eclipse中的服務器選項卡,右鍵單擊tomcat實例並選擇屬性,然後單擊「切換位置」。並應用更改,重新啓動服務器。 –

回答

1

儘管需要從Http訪問保護的資源放置在WEB-INF下。 通常jsps用於向用戶演示,這就是爲什麼他們被放置在WEB-INF之外的原因。 但由於某些原因,如果您堅持要將jsp文件保存在WEB-INF文件夾下,而不是創建一個servlet,請在註冊鏈接中調用該servlet。

在servlet中,你需要使用的RequestDispatcher重定向到您的register.jsp

保持你的servlet代碼是這樣的:(我假設你的寄存器。JSP是速效直屬WEB-INF文件夾)

@WebServlet("/ControlFlowServlet") 
public class ControlFlowServlet extends HttpServlet { 
    protected void doGet(HttpServletRequest request, 
      HttpServletResponse response) throws ServletException, IOException { 
     RequestDispatcher dispatcher = getServletContext() 
       .getRequestDispatcher("/WEB-INF/register.jsp"); 
     dispatcher.forward(request, response); 
    } 

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

} 

加入這個映射在你的web.xml:

<servlet-mapping> 
    <servlet-name>ControlFlowServlet</servlet-name> 
    <url-pattern>/register</url-pattern> 
    </servlet-mapping> 

而在你的index.jsp保持這樣的:

<a href="${pageContext.request.contextPath}/register">New User Register here...!!!</a><br> 

欲瞭解更多信息,請仔細閱讀this解釋。 如果您需要任何澄清,請讓我知道。

+0

謝謝Shailesh,我現在明白了。謝謝你的迅速和善意的迴應。 – jahnavi

相關問題