это, наверное, глупый вопрос, но я действительно застрял и нуждаюсь в вашей помощи. Я сделал сервлет Java, который обрабатывает запросы демонстрационного приложения CRUD. Это мой сервлет:
public class AuthorController extends HttpServlet {
private static final long serialVersionUID = 1L;
private AuthorDAO authorDAO;
public void init() {
String jdbcURL = getServletContext().getInitParameter("jdbcURL");
String jdbcUsername = getServletContext().getInitParameter("jdbcUsername");
String jdbcPassword = getServletContext().getInitParameter("jdbcPassword");
authorDAO = new AuthorDAO(jdbcURL, jdbcUsername, jdbcPassword);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getServletPath();
System.out.println(action);
try {
switch (action) {
case "/newAuthor":
showNewForm(request, response);
break;
case "/insertAuthor":
insertAuthor(request, response);
break;
case "/deleteAuthor":
deleteAuthor(request, response);
break;
case "/editAuthor":
showEditForm(request, response);
break;
case "/updateAuthor":
updateAuthor(request, response);
break;
default:
listAuthor(request, response);
break;
}
} catch (SQLException ex) {
throw new ServletException(ex);
}
}
/* List all authors */
private void listAuthor(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException, ServletException {
List<Author> listAuthor = authorDAO.listAllAuthors();
request.setAttribute("listAuthors", listAuthor);
RequestDispatcher dispatcher = request.getRequestDispatcher("AuthorList.jsp");
dispatcher.forward(request, response);
}
/* Show authors form */
private void showNewForm(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("AuthorForm.jsp");
dispatcher.forward(request, response);
}
/* Show authors edit form */
private void showEditForm(HttpServletRequest request, HttpServletResponse response)
throws SQLException, ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
Author existingAuthor = authorDAO.getAuthor(id);
RequestDispatcher dispatcher = request.getRequestDispatcher("AuthorForm.jsp");
request.setAttribute("author", existingAuthor);
dispatcher.forward(request, response);
}
/* Inserting a new author */
private void insertAuthor(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException {
String author_name = request.getParameter("author_name");
int author_age = Integer.parseInt(request.getParameter("author_age"));
String author_country = request.getParameter("author_country");
Author newAuthor = new Author(author_name, author_age, author_country);
authorDAO.insertAuthor(newAuthor);
response.sendRedirect("listAuthor");
}
/* Updating an existing author */
private void updateAuthor(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
String author_name = request.getParameter("author_name");
int author_age = Integer.parseInt(request.getParameter("author_age"));
String author_country = request.getParameter("author_country");
Author author = new Author(id, author_name, author_age, author_country);
authorDAO.updateAuthor(author);
response.sendRedirect("listAuthor");
}
/* Deleting an existing author */
private void deleteAuthor(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
Author author = new Author(id);
authorDAO.deleteAuthor(author);
response.sendRedirect("listAuthor");
}
}
А это мой файл web.xml:
<servlet>
<servlet-name>AuthorController</servlet-name>
<servlet-class>org.demo.AuthorController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AuthorController</servlet-name>
<url-pattern>/listAuthor</url-pattern>
</servlet-mapping>
Однако, когда я пытаюсь получить доступ к http: // localhost: 8080 / listAuthor / newAuthor, Почтальон выдает мне ошибку 404. Что я делаю неправильно? Я совершенно запутался в этом отображении и не могу заставить его работать. Я могу получить доступ к: http: // localhost: 8080 / listAuthor и перечислить свою таблицу с авторами, но не могу добавить новую.
<url-pattern>/listAuthor</url-pattern>
делает в<servlet-mapping>
? - person Sotirios Delimanolis   schedule 22.11.2020http://localhost:8080/AuthorList.jsp
из браузера? - person Jude Niroshan   schedule 22.11.2020