JavaWeb常见错误

1.<a href="customerServlet?method=add">Add</a>  这里method=add不能有空格,否则报错(空指针异常)

 1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 2     pageEncoding="ISO-8859-1"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <a href="customerServlet?method=add">Add</a>
11     <br>
12     <br>
13 
14     <a href="customerServlet?method=query">Query</a>
15     <br>
16     <br>
17 
18     <a href="customerServlet?method=delete">Delete</a>
19     <br>
20     <br>
21 
22 </body>
23 </html>
View Code
 1 package com.ouyang.dao;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.annotation.WebServlet;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 
11 @WebServlet("/customerServlet")
12 public class CustomerServlet extends HttpServlet {
13     private static final long serialVersionUID = 1L;
14 
15     protected void doGet(HttpServletRequest request,
16             HttpServletResponse response) throws ServletException, IOException {
17         doPost(request, response);
18     }
19 
20     protected void doPost(HttpServletRequest request,
21             HttpServletResponse response) throws ServletException, IOException {
22         String method = request.getParameter("method");
23 
24         switch (method) {
25         case "add":
26             add(request, response);
27             break;
28         case "query":
29             query(request, response);
30             break;
31         case "delete":
32             delete(request, response);
33             break;
34 
35         }
36 
37     }
38 
39     private void delete(HttpServletRequest request, HttpServletResponse response)
40             throws ServletException, IOException {
41         System.out.println("delete");
42     }
43 
44     private void query(HttpServletRequest request, HttpServletResponse response)
45             throws ServletException, IOException {
46         System.out.println("query");
47     }
48 
49     private void add(HttpServletRequest request, HttpServletResponse response)
50             throws ServletException, IOException {
51         System.out.println("add");
52 
53     }
54 
55 }
View Code

 2.url-pattern 中在建立Servlet时前面不应该加"/".否则会报错

转载于:https://www.cnblogs.com/Ouyangan/p/4189070.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值