Everyday前进一步 2017-07-26 12:51 采纳率: 0%
浏览 2575

请问如何通过servlet将response结果返回至提交页面?

现在希望通过servlet连接前后端,效果为:点击前端button即可调用servlet查询数据库中信息是否重复。
小弟目前不清楚的内容有以下几点:
1. 想直接在 newOEM_Customer.html 中通过按钮提交form调用servlet,并将结果返回到这个页面中
2.web.xml该如何配置

之前做过通过servlet跳转到其他页面的项目,但将结果返回至提交页面上这还是头一次,请问是否可行?我将各部分代码粘贴于下方,请各位大侠不吝相助,小弟感激不尽!

**newOEM_Customer.html **

                            <div class="tab-content">
                                <div class="tab-pane active" id="1">
                                <form id="edit-profile" action="/zzz_SchneiderDatabaseApplication/newOEMcustomer" class="form-horizontal" method="get" />
                                    <fieldset>
                                        <div class="control-group">                                         
                                            <label class="control-label" for="username">OEM Code</label>
                                            <div class="controls">
                                                <input type="text" class="input-medium disabled" id="OEMCode" value="OEM-XXXXX" disabled=" " />
                                                <p class="help-block">请输入OEM客户基本信息,如为新客户,系统将自动生成OEM Code.</p>
                                            </div> <!-- /controls -->               
                                        </div> <!-- /control-group -->


                                        <div class="control-group">                                         
                                            <label class="control-label" for="firstname">OEM客户名称</label>
                                            <div class="controls">
                                                <input type="text" class="input-xxlarge" id="name" name="name" value="请输入全称" onclick="this.value='';focus()" />
                                            </div> <!-- /controls -->               
                                        </div> <!-- /control-group -->

                                        <div class="control-group">                                         
                                            <label class="control-label" for="email">通讯地址</label>
                                            <div class="controls">
                                                <input type="text" class="input-xxlarge" id="adress" name="adress" value="请输入完整地址信息" onclick="this.value='';focus()"/>
                                            </div> <!-- /controls -->               
                                        </div> <!-- /control-group -->

                                        <div class="control-group">                                         
                                            <label class="control-label" for="lastname">省份</label>
                                            <div class="controls">
                                                <input type="text" class="input-medium" id="province" name="province" value="请输入汉字全称" onclick="this.value='';focus()" />
                                            </div> <!-- /controls -->               
                                        </div> <!-- /control-group -->

                                        <div class="control-group">                                         
                                            <label class="control-label" for="lastname">城市</label>
                                            <div class="controls">
                                                <input type="text" class="input-medium" id="city" name="city" value="请输入汉字全称" onclick="this.value='';focus()" />
                                            </div> <!-- /controls -->               
                                        </div> <!-- /control-group -->

                                        <p><%${message}%></p>
                                        <br /><br />



                                            <button type="submit"  class="btn btn-primary" >查询信息</button> 

                                            <a href="./OEMcustomer.jsp"><button class="btn" type="button">取消</button></a>

                                    </fieldset>
                                </form>
                                </div>

Servlet

@WebServlet(name="newOEMcustomer",urlPatterns="/newOEM_Customer")
public class NewOEMcustomerController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public NewOEMcustomerController() {
        super();
        // TODO Auto-generated constructor stub
    }
    public void init() {
        try {
            super.init();
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html");
        String CustomerName=req.getParameter("name");
        String CustomerProvince=req.getParameter("province");
        String CustomerCity=req.getParameter("city");
        OEM_Customer_dao customerDao=new OEM_Customer_dao();
        System.out.println("Get req!");
        try {
            String result=customerDao.findCustomerByBasicInformation(CustomerName, CustomerProvince, CustomerCity);
            PrintWriter out = res.getWriter();
            req.setAttribute("message", result); // This will be available as ${message}
            req.getRequestDispatcher("/PAGES/newOEM_Customer.html").forward(req, res);
            out.println(result);
            out.flush();
            out.close();
            System.out.println("Send Resp...");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

web.XML

 <?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<servlet>
    <servlet-name>newOEMcustomer</servlet-name> 
    <servlet-class>controller.NewOEMcustomerController</servlet-class>
</servlet>

<servlet-mapping>
        <servlet-name>newOEMcustomer</servlet-name>
        <url-pattern>/zzz_SchneiderDatabaseApplication/PAGES/newOEMcustomer</url-pattern>
</servlet-mapping>

</web-app>
  • 写回答

1条回答 默认 最新

  • JPF1024 2017-07-26 15:55
    关注

    可以的,

     req.getRequestDispatcher("/PAGES/newOEM_Customer.html").forward(req, res);
    

    要改成jsp页面才可以拿到req,和res吧,不然你怎么获取返回的信息,这样直接就可以获取到.

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大