tomcat不能跳转,求大神帮忙

帅小陶 2013-12-16 08:31:55
登录界面login,验证处理类logincl,欢迎界面welcome,配置xml了
在tomcat中不能完成验证
希望大神帮忙

package com.txj.servlet.demo;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Login extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
// 中文乱码
res.setContentType("text/html;charset=gbk");
PrintWriter pw = res.getWriter();
pw.println("<html>");
pw.println("<body>");
pw.println("<h1>登录界面</h1>");
pw.println("<form action=LoginCL method='post'>");
pw.println("用户名:<input type=text name=username><br>");
pw.println("密码:<input type=password name=passwd><br>");
pw.println("<input type=submit value=login><br>");
pw.println("</form>");
pw.println("</body>");
pw.println("</html>");
}

public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
this.doGet(req, res);
}
}


package com.txj.servlet.demo;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginCL extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
// 接收用户名和密码
String u = req.getParameter("username").trim();
String p = req.getParameter("passwd").trim();
// 验证
if (u.equals("txj") && p.equals("123")) {
// 合法E
// res.sendRedirect("welcome");//你要的servlet的url
res.sendRedirect("welcome1");
} else {
// 不合法,跳转
res.sendRedirect("login1");// 你要的servlet的url
}
}

public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

this.doGet(req, res);
}
}



package com.txj.servlet.demo;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Welcome extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter pw = res.getWriter();
pw.println("Welcome to my home !!!");
}

public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
this.doGet(req, res);
}
}




<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"
metadata-complete="true">

<display-name>Tomcat Manager Application</display-name>
<description>
A scriptable management web application for the Tomcat Web Server;
Manager lets you view, load/unload/etc particular web applications.
</description>

<servlet>
<!--give servlet name 1 -->
<servlet-name>login</servlet-name>
<!--servlet track,(bao name and class name)-->
<servlet-class>com.txj.servlet.demo.Login</servlet-class>
</servlet>

<!-- Define the Manager Servlet Mapping -->
<servlet-mapping>
<servlet-name>login</servlet-name>
<!--on browser ,intput servlet's url-->
<url-pattern>/login</url-pattern>
</servlet-mapping>

<servlet>
<!--give servlet name 2 -->
<servlet-name>logincl</servlet-name>
<!--servlet track,(bao name and class name)-->
<servlet-class>com.txj.servlet.demo.LoginCL</servlet-class>
</servlet>

<!-- Define the Manager Servlet Mapping -->
<servlet-mapping>
<servlet-name>logincl</servlet-name>
<!--on browser ,intput servlet's url-->
<url-pattern>/logincl</url-pattern>
</servlet-mapping>

<servlet>
<!--give servlet name 3 -->
<servlet-name>welcome</servlet-name>
<!--servlet track,(bao name and class name)-->
<servlet-class>com.txj.servlet.demo.Welcome</servlet-class>
</servlet>

<!-- Define the Manager Servlet Mapping -->
<servlet-mapping>
<servlet-name>welcome</servlet-name>
<!--on browser ,intput servlet's url-->
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
...全文
435 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
帅小陶 2013-12-16
  • 打赏
  • 举报
回复
谢谢大神们!!!
帅小陶 2013-12-16
  • 打赏
  • 举报
回复
引用 5 楼 NNTT2010 的回复:
pw.println("<form action=LoginCL method='post'>"); 改成 pw.println("<form action='logincl' method='post'>"); 你试试。
Thank You 改正后,结果出来了!
姜小白- 2013-12-16
  • 打赏
  • 举报
回复
引用 7 楼 yxngthj 的回复:
我配置welcom1 和 login1是刚才打错了

 if (u.equals("txj") && p.equals("123")) {
// 合法E 
res.sendRedirect("welcome");       
 } else {             // 不合法,跳转          
   res.sendRedirect("login");// 你要的servlet的url 
}
这样不行,我试过了,
按五楼操作改,这个错误是下一个错误,当前不能跳转是5楼指出的那个问题
帅小陶 2013-12-16
  • 打赏
  • 举报
回复
我配置welcom1 和 login1是刚才打错了

 if (u.equals("txj") && p.equals("123")) {
// 合法E 
res.sendRedirect("welcome");       
 } else {             // 不合法,跳转          
   res.sendRedirect("login");// 你要的servlet的url 
}
这样不行,我试过了,
姜小白- 2013-12-16
  • 打赏
  • 举报
回复
引用 5 楼 NNTT2010 的回复:
pw.println("<form action=LoginCL method='post'>"); 改成 pw.println("<form action='logincl' method='post'>"); 你试试。
火眼金睛 都没注意到那个大小写错误
长笛党希望 2013-12-16
  • 打赏
  • 举报
回复
pw.println("<form action=LoginCL method='post'>"); 改成 pw.println("<form action='logincl' method='post'>"); 你试试。
姜小白- 2013-12-16
  • 打赏
  • 举报
回复
 if (u.equals("txj") && p.equals("123")) {
            // 合法E
            // res.sendRedirect("welcome");//你要的servlet的url
            res.sendRedirect("welcome1");
        } else {
            // 不合法,跳转
            res.sendRedirect("login1");// 你要的servlet的url
        }
这里是welcom1 和 login1 你的web.xml里面根本这不到对应的url-pattern,楼主检查下配置文件,java类中的跳转和配置文件不匹配
帅小陶 2013-12-16
  • 打赏
  • 举报
回复
if (u.equals("txj") && p.equals("123")) 我就简单的直接匹配 logiincl不能起作用 不知道原因
  • 打赏
  • 举报
回复
断点调试,是不是已经取到用户名和密码了?

81,120

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧