servlet初学者,出现个小问题,求解释修改
写了servlet小程序,输入完sp和123后,没有跳转到wel界面。。。为什么??Login.java
package com.liu;
import javax.servlet.http.*;
import java.io.*;
public class Login extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) {
// 处理业务逻辑
//req:用于获得浏览器的的信息
//res:用于向浏览器返回信息
try {
//中文乱码
res.setContentType("text/html;charset=gbk");
PrintWriter pw = res.getWriter();
//返回登陆界面
pw.println("<html>");
pw.println("<body>");
pw.println("<h1>登陆界面</h1>");
//ACTION 代表着用来接纳表单数据的jsp或着servlet,写url名称
pw.println("<form atcion=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=loing><br>");
pw.println("</form>");
pw.println("</body>");
pw.println("</html>");
} catch (Exception e) {
e.printStackTrace();
}
}
// 处理Post请求
public void doPost(HttpServletRequest req, HttpServletResponse res) {
this.doGet(req, res);
}
}
LoginCl.java
package com.liu;
import javax.servlet.http.*;
import java.io.*;
public class Login extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) {
// 处理业务逻辑
//req:用于获得浏览器的的信息
//res:用于向浏览器返回信息
try {
//中文乱码
res.setContentType("text/html;charset=gbk");
PrintWriter pw = res.getWriter();
//返回登陆界面
pw.println("<html>");
pw.println("<body>");
pw.println("<h1>登陆界面</h1>");
//ACTION 代表着用来接纳表单数据的jsp或着servlet,写url名称
pw.println("<form atcion=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=loing><br>");
pw.println("</form>");
pw.println("</body>");
pw.println("</html>");
} catch (Exception e) {
e.printStackTrace();
}
}
// 处理Post请求
public void doPost(HttpServletRequest req, HttpServletResponse res) {
this.doGet(req, res);
}
}
Wel.java
//欢迎界面
package com.liu;
import javax.servlet.http.*;
import java.io.*;
public class Wel extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) {
// 处理业务逻辑
try {
PrintWriter pw=res.getWriter();
pw.println("welcome");
} catch (Exception e) {
e.printStackTrace();
}
}
// 处理Post请求
public void doPost(HttpServletRequest req, HttpServletResponse res) {
this.doGet(req, res);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Servlet</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>Login</display-name>
<servlet-name>Login</servlet-name>
<servlet-class>com.liu.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>LoginCl</display-name>
<servlet-name>LoginCl</servlet-name>
<servlet-class>com.liu.LoginCl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginCl</servlet-name>
<url-pattern>/LoginCl</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>Wel</display-name>
<servlet-name>Wel</servlet-name>
<servlet-class>com.liu.Wel</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Wel</servlet-name>
<url-pattern>/Wel</url-pattern>
</servlet-mapping>
</web-app> --------------------编程问答-------------------- 发错了LoginCl.java
LoginCl.java
package com.liu;--------------------编程问答-------------------- <url-pattern>/LoginCl</url-pattern>这个写的的有问题 --------------------编程问答-------------------- 你那个Login.java大概20行左右,里面写得有问题,试试
import javax.servlet.http.*;
import java.io.*;
public class LoginCl extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) {
// 处理业务逻辑
try {
//接受用户名和密码
String u=req.getParameter("username");
String p=req.getParameter("passwd");
if(u.equals("sp")&&p.equals("123")){
res.sendRedirect("Wel");
}else{
//跳转语句,要写你想跳到哪里的url!!!!
res.sendRedirect("Login");
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 处理Post请求
public void doPost(HttpServletRequest req, HttpServletResponse res) {
this.doGet(req, res);
}
}
pw.println("<form atcion='LoginCl' method='post'>");或者转义双引号
pw.println("<form atcion=\"LoginCl\" method=\"post\">");诸如这种问题,你下面还有很多。 --------------------编程问答-------------------- 如果是404错误的话,你就要检查一下web.xml里面又没有配置正确,路径什么的。 --------------------编程问答-------------------- form的属性是action拼写错误,不是atcion,坑爹的错误 --------------------编程问答--------------------
犀利的眼神
补充:Java , Web 开发