为什么我的不能咋控制台输出哎

为什么我的不能咋控制台输出哎

package com.imooc.web.action;


import java.io.IOException;

import java.util.List;


import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import com.imooc.domain.Category;

import com.imooc.service.CategoryService;

import com.imooc.service.impl.CategoryServiceImpl;


/**

 * Servlet implementation class CategoryServlet

 */

@WebServlet("/CategoryServlet")

public class CategoryServlet extends HttpServlet {

private static final long serialVersionUID = 1L;


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

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

//请求路径:localhost:8080/shop/CategoryServlet?method=findAll

String methodName=request.getParameter("method");

if("findAll".equals(methodName)) {

//查询所有分类

findAll(request,response);

//跳转到添加页面

}else if("saveUI".equals(methodName)) {

saveUI(request,response);

//保存分类的方法

}else if("save".equals(methodName)) {

save(request,response);

}

}

//后台分类管理,保存分类的方法

private void save(HttpServletRequest request, HttpServletResponse response) throws IOException {

//1.接受数据

String cname=request.getParameter("cname");

String cdesc=request.getParameter("cdesc");

System.out.println("接收的数据为:"+cname+"  "+cdesc);

//2.封装数据

Category category=new Category();

category.setCname(cname);

category.setCdesc(cdesc);

//3.调用业务层处理数据

CategoryService categoryService=new CategoryServiceImpl();

categoryService.save(category);

//4.页面跳转

response.sendRedirect(request.getContentType()+"/CategoryServlet?method=findAll");

}

//后台分类管理跳转到添加页面

private void saveUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

request.getRequestDispatcher("/admin/category_add.jsp").forward(request, response);

}

//后台分类管理查询所有分类的方法

private void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//接受参数

//封装数据

//调用业务成处理数据

System.out.println("CategoryServlet的findALL方法执行了");

CategoryService categoryService=new CategoryServiceImpl();

List<Category> list=categoryService.findAll();

for(Category category:list){

System.out.println(category);

}

//页面跳转

request.setAttribute("list", list);

//请求转发

request.getRequestDispatcher("/admin/category_list.jsp").forward(request,response);

}


/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

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

// TODO Auto-generated method stub

doGet(request, response);

}


}

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>



<!-- Mirrored from admindesigns.com/demos/absolute/1.1/admin_forms-validation.html by HTTrack Website Copier/3.x [XR&CO'2014], Thu, 06 Aug 2015 02:56:15 GMT -->

<head>

    <!-- Meta, title, CSS, favicons, etc. -->

    <meta charset="utf-8">


<title>油画商城--添加分类</title>


<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/skin/default_skin/css/theme.css">

<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/admin-tools/admin-forms/css/admin-forms.css">

<link rel="shortcut icon" href="${pageContext.request.contextPath }/assets/img/favicon.ico">

</head>


<body class="admin-validation-page" data-spy="scroll" data-target="#nav-spy" data-offset="200">

<div id="main">

 <%@ include file="header.jsp" %>

 

 <%@ include file="left.jsp" %>

 

    <section id="content_wrapper">

<section id="content" class="table-layout animated fadeIn">

    <div class="tray tray-center">

        <div class="content-header">

            <h2> 添加分类 </h2>

            <p class="lead"></p>

        </div>

        <div class="admin-form theme-primary mw1000 center-block" style="padding-bottom: 175px;">

            <div class="panel heading-border">

                <form id="admin-form" name="addForm" action="${pageContext.request.contextPath }/CategoryServlet" method="post">

                <input type="hidden" name="method" value="save">

                    <div class="panel-body bg-light">

                        <div class="section-divider mt20 mb40">

                            <span> 基本信息 </span>

                        </div>

                        <div class="section row">

                           

                        <div class="section row">

<div class="col-md-2"></div>

<div class="col-md-1">

                                <label for="sn" class="field prepend-icon">

                                    <label for="sn" class="field-icon">

名称

                                    </label>

                                </label>

                            </div>

                            <div class="col-md-6">

                                <label for="sn" class="field">

                                    <input id="sn" name="cname" class="gui-input" placeholder="名称" type="text" value="imooc"/>

                                </label>

                            </div>

                        </div>

                        <div class="section row">

<div class="col-md-2"></div>

<div class="col-md-1">

                                <label for="sn" class="field prepend-icon">

                                    <label for="sn" class="field-icon">

描述

                                    </label>

                                </label>

                            </div>

<div class="col-md-6">

<label for="address" class="field">

<input id="address" name="cdesc" class="gui-input" placeholder="描述" type="text" value=""/>

</label>

</div>

                        </div>

                        <div class="panel-footer text-center">

                            <button type="submit" class="button"> 保存 </button>

                            <button type="button" class="button" onclick="javascript:window.history.go(-1);"> 返回 </button>

                        </div>

                    </div>

                </form>

            </div>

        </div>

    </div>

</section>


</section>

</div>

<style>

    /* demo page styles */

    body { min-height: 2300px; }


    .content-header b,

    .admin-form .panel.heading-border:before,

    .admin-form .panel .heading-border:before {

        transition: all 0.7s ease;

    }

    /* responsive demo styles */

    @media (max-width: 800px) {

        .admin-form .panel-body { padding: 18px 12px; }

    }

</style>


<style>

    .ui-datepicker select.ui-datepicker-month,

    .ui-datepicker select.ui-datepicker-year {

        width: 48%;

        margin-top: 0;

        margin-bottom: 0;


        line-height: 25px;

        text-indent: 3px;


        color: #888;

        border-color: #DDD;

        background-color: #FDFDFD;


        -webkit-appearance: none; /*Optionally disable dropdown arrow*/

    }

</style>

<script src="vendor/jquery/jquery-1.11.1.min.js"></script>

<script src="vendor/jquery/jquery_ui/jquery-ui.min.js"></script>

<script src="assets/admin-tools/admin-forms/js/jquery.validate.min.js"></script>

<script src="assets/admin-tools/admin-forms/js/additional-methods.min.js"></script>

<script src="assets/admin-tools/admin-forms/js/jquery-ui-datepicker.min.js"></script>

<script src="assets/js/utility/utility.js"></script>

<script src="assets/js/demo/demo.js"></script>

<script src="assets/js/main.js"></script>

<script type="text/javascript" src="js/pages.js"></script>

<script type="text/javascript" src="js/items.js"></script>

</body>

</html>


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

3回答
好帮手慕阿慧 2020-10-09 19:28:04

同学你好,建议同学在doGet()方法中输出一下methodName值,看一下是不是save。同学可以在程序中添加断点,如下:

http://img1.sycdn.imooc.com//climg/5f80491409be5bf010450558.jpg

以debug启动Tomcat,如下:

http://img1.sycdn.imooc.com//climg/5f804931098dd34f05710325.jpg

跟踪一下程序,看一下程序的执行流程。

祝学习愉快~

提问者 怀夢 2020-10-09 19:14:06

private void save(HttpServletRequest request, HttpServletResponse response) throws IOException {

//1.接受数据

String cname=request.getParameter("cname");

String cdesc=request.getParameter("cdesc");

System.out.println("接收的数据为:"+cname+"  "+cdesc);

}

好帮手慕阿慧 2020-10-09 11:26:53

同学你好,

1、同学指的是不能输出接收的数据cname和cdesc吗?如果是,老师测试同学代码是可以接收到的,可以新增分类。

运行效果如下:

http://img1.sycdn.imooc.com//climg/5f7fcf8909dc54cb08520370.jpg

同学save()方法中重定向获得项目请求路径应该调用getContextPath()方法。

参考代码如下:

http://img1.sycdn.imooc.com//climg/5f7fcfd9098d53a908620399.jpg

2、如果问题没有解决,建议同学清理一下Tomcat,如下:

http://img1.sycdn.imooc.com//climg/5f7fd00b09e1766c05580210.jpg

重启Tomcat,清理一下浏览器缓存或者换个浏览器访问试试。

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

  • 提问者 怀夢 #1
    都试了,改的也改了就还是不能添加,控制台也不输出奇怪了就
    2020-10-09 11:58:29
  • 提问者 怀夢 #2
    您看下这个还有什么可能性导致不能进行添加呢,您这边运行都没问题我这边就是不行,添加的时候就跳转到空白页面,控制台也不输出
    2020-10-09 14:32:54
  • 提问者 怀夢 #3
    老师您好我测试了一下,当在addCourse.jsp中把form的action改为="${pageContext.request.contextPath }/CategoryServlet?method=findAll"就可以执行findAll方法,当换成save却不能执行,这应该是代表我的save方法有问题吧,但是我save方法的代码看着也是完全没问题的哎怎么就执行不了呢
    2020-10-09 19:13:10
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师