【问题标题】:Search enging using java ee (.jsp .java bean.java)使用 java ee (.jsp .java bean.java) 的搜索引擎
【发布时间】:2026-01-08 07:45:01
【问题描述】:

您好,我想知道如何在不使用数据库的情况下制作一个简单的搜索引擎

我有 3 页 StudentSearch.jsp、StudentSearchController.java StudentSearchBean.java

我不知道从哪里开始在 java 中使用简单的 mvc。谢谢。

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();


try{

    String  pro_code  = request.getParameter("pro_code");
    String  name  = request.getParameter("name");
    String  price  = request.getParameter("price");

    ArrayList pList = null;
    ArrayList proList = new ArrayList();
    pList  = new ArrayList();

    pList.add(pro_code + ("0000001"));
    pList.add(name + ("sample"));
    pList.add(price + ("1000"));
    proList.contains(pList);

    request.setAttribute("proList" +  session, proList);        

    RequestDispatcher rd = request.getRequestDispatcher("/studentSearch.jsp");
    rd.forward(request, response);

} catch (Exception e) {
   e.printStackTrace();
}

下面是我的jsp,希望我补充了一些信息(对不起我的英语不好)。

<%
int count=0;
String color = "blue";

if(request.getAttribute("proList")!=null) {

ArrayList plist = (ArrayList)request.getAttribute("proList");
    Iterator lst = plist.iterator();

    while(lst.hasNext())
    {
        if((count%2)==0)
        {
            color="yellow";
        }
        else
        {
            color="brown";
        }
        count++;
        ArrayList proList = (ArrayList)lst.next();

        %>

<%=proList.get(0)%>
<%=proList.get(1)%>
<%=proList.get(2)%>

<%
    }
}

%>



SEVERE: Error starting static Resources java.lang.IllegalArgumentException: Document base C:\Users\cire\Documents\softdev-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\StrutsStarter does not exist or is not a readable directory
at org.apache.naming.resources.FileDirContext.setDocBase(FileDirContext.java:140)
at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:4894)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5074)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1568)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1558)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

【问题讨论】:

  • 我建议你先读这本书,Head First Servlets and JSP你会在读完第4章之前做这个搜索引擎。
  • 我没有钱买这本书,我已经创建了 3 页,但我不知道我的代码有什么问题,它没有显示在网络浏览器中
  • 你能告诉我们错误是什么吗?如果有疑问,请发布异常和您请求的 URL

标签: java jakarta-ee


【解决方案1】:

第一个问题是您在这里遇到的异常与代码无关。似乎是服务器启动时的问题。

但是在代码前面

proList.contains(pList);

这只是测试proList 包含对象pList 它不会

如果你想要proList 中的所有pList 元素,那么:

proList.addAll(pList);

如果您想将pList 作为元素添加到proList,则:

proList.add(plist);

下一期是

request.setAttribute("proList" +  session, proList);

你想在这里做什么?我猜你可能会怎么做:

request.setAttribute("proList", proList);

事实上,您可能需要重新考虑上面的大部分代码,然后选择以下内容:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String  pro_code  = request.getParameter("pro_code");
    String  name  = request.getParameter("name");
    String  price  = request.getParameter("price");

    // do something usefull with the parameters here

    // expose the results to the jsp like so:
    request.setAttribute("pro_code", pro_code);  
    request.setAttribute("name", name);  
    request.setAttribute("price",price); 
    RequestDispatcher rd = request.getRequestDispatcher("/studentSearch.jsp");
    rd.forward(request, response);
}

注意缺少catch(Exception e)

【讨论】:

  • 我们需要更多信息,我们几乎无法理解您的程序,很难提供任何有意义的答案。阅读tinyurl.com/so-hints,然后回来重做你的问题(或问另一个问题)