JSP开发问题

zhangfengyi 2015-11-03 09:10:01
第一次开发JSP程序,按照网上介绍的教程做的表单提交程序,点提交没有反应,不知道是哪里的错,怎样调试,把页面贴出来,帮忙分析一下。
JSP页面:

JSP代码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
<format action="TestServlet">
w3c在线教程:
<input type="text" name="weburl" size="100"/><br/>
<input type="submit" value="提交"/>
</format>
</body>
</html>

TestServlet代码:

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

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


public class TestServlet extends HttpServlet {

private static final long serialVersionUID=-1575386983723846021L;
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String weburl=request.getParameter("weburl");
System.out.print(weburl);
if(weburl!=null && weburl.equals("http://www.w3csschool.com"))
{
request.setAttribute("weburl", "你输入的是W3C在线教程网");
}
else
{
request.setAttribute("weburl", "你输入的网址在数据库中未有记录");
}
RequestDispatcher dispatcher=request.getRequestDispatcher("result.jsp");
dispatcher.forward(request, response);
}

}


web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>TestServlet</servlet-name>
<servlet-class>TestServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/servlet/TestServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


result.jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>结果是:</title>
</head>

<body>
<%=request.getAttribute("weburl")%>
</body>
</html>
...全文
791 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangfengyi 2015-11-05
  • 打赏
  • 举报
回复
这样就可以了 String name=new String(request.getParameter("Name").getBytes("ISO-8859-1"),"UTF-8");
zhangfengyi 2015-11-05
  • 打赏
  • 举报
回复
新问题,jsp 页面与servlet页面都设置了utf-8编码,为什么中文字符还是乱码 jsp页面:

<%@page pageEncoding="utf-8" contentType="text/html;charset=utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">d
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
servlet页面:

response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
zhangfengyi 2015-11-03
  • 打赏
  • 举报
回复
TestServlet.java如下,没有返回结果,我怎样调试

private static final long serialVersionUID=-1575386983723846021L;
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//String merchID=request.getParameter("MerchID");
		
		//if(merchID!=null)
		//{
		//	request.setAttribute("merchID", "你输入的是W3C在线教程网");
		//}
		//else
		//{
		//	request.setAttribute("merchID", "你输入的网址在数据库中未有记录");
		//}
		//RequestDispatcher dispatcher=request.getRequestDispatcher("result.jsp");
		//dispatcher.forward(request, response);
		
		try
		{
			//String merchid=request.getParameter("MerchID");
			//String msgid=request.getParameter("MsgID");
			//String cardno=request.getParameter("CardID");
			//System.out.println(merchid);
			//RequestDispatcher dispatcher=request.getRequestDispatcher("result.jsp");
			//dispatcher.forward(request, response);
			String cardno="6228480030976274";
			String certino="410711199002251";
			String name="王*峰";
			String phone="13918315*";
			
			String limitdate="";
			String cvn2="";
			
			String msgid=getCurrentDateTime();
			String merchid="33350018";
			String certitype="01";
			String cerPwd="1234567890";
			String deskey="6350105e40013e7de0530b14640a74e2";
			
			String url="http://101.254.166.186/certification/check";
			int conntimeout=50000;
			int sotimeout=60000;
			
			CertiRequest req=new CertiRequest();
			req.setMerchid(merchid);
			req.setMsgid(msgid);
			req.setCardno(cardno);
			req.setCertitype(certitype);
			req.setCertino(certino);
			req.setName(name);
			req.setPhone(phone);
			req.setLimitdate(limitdate);
			req.setCvn2(cvn2);
			req.setDeskey(deskey);
			req.setConntimeout(conntimeout);
			req.setSotimeout(sotimeout);
			req.setUrl(url);
			
			RequestService serv=new RequestService();
			CertiResponse resp;
			try
			{
				resp=serv.send(req);
			}
			catch(Exception e)
			{
				e.printStackTrace();
				return;
			}
			System.out.println("返回数据:"+resp.toString());
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public static String getCurrentDateTime()
	{
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyddHHmmss");
		return String.valueOf(dateFormat.format(new Date()));
	}
	
	public static String productFigure()
	{
		Random rnd=new Random();
		int num=rnd.nextInt();
		if(num<=0 || num>999999)
		{
			return productFigure();
		}
		return String.valueOf(num);
	}
zhangfengyi 2015-11-03
  • 打赏
  • 举报
回复
问题是解决了,还有其他问题
qq_17553329 2015-11-03
  • 打赏
  • 举报
回复
好像解决了
I_am_a_Beginner 2015-11-03
  • 打赏
  • 举报
回复
进来学习!
Usher丶AckMan 2015-11-03
  • 打赏
  • 举报
回复
右击项目属性,不是有个java build paths,里面加就可以了
zhangfengyi 2015-11-03
  • 打赏
  • 举报
回复
在TestServlet.java中怎样引用jar包

2015/09/23  19:04           263,965 commons-codec-1.9.jar
2015/09/23  18:19           305,001 commons-httpclient-3.1.jar
2015/09/23  19:03            62,050 commons-logging-1.1.3.jar
2015/01/13  10:14           313,898 dom4j-1.6.1.jar
2015/01/11  13:45           489,884 log4j-1.2.17.jar
2015/10/10  11:33            23,573 payCheck-1.0.1.jar
zhangfengyi 2015-11-03
  • 打赏
  • 举报
回复
是路径的问题,改了下路径就可以了,配置文件中的路径<url-pattern>/TestServlet</url-pattern>,改成这样的就可以了
  • 打赏
  • 举报
回复
你把action的值改一下,改成这个/servlet/TestServlet
sinat_31535993 2015-11-03
  • 打赏
  • 举报
回复
引用 2 楼 zhangfengyi 的回复:
[quote=引用 1 楼 sinat_31535993 的回复:] <format action="TestServlet"> w3c在线教程: <input type="text" name="weburl" size="100"/><br/> <input type="submit" value="提交"/> </format> 第一次见form表单写成format的
写错了,问题又来了,这个路径如何写[/quote] <url-pattern>/servlet/TestServlet</url-pattern>这不就是你自己设置的路径!
zhangfengyi 2015-11-03
  • 打赏
  • 举报
回复
引用 1 楼 sinat_31535993 的回复:
<format action="TestServlet">
w3c在线教程:
<input type="text" name="weburl" size="100"/><br/>
<input type="submit" value="提交"/>
</format>
第一次见form表单写成format的

写错了,问题又来了,这个路径如何写
sinat_31535993 2015-11-03
  • 打赏
  • 举报
回复
<format action="TestServlet"> w3c在线教程: <input type="text" name="weburl" size="100"/><br/> <input type="submit" value="提交"/> </format> 第一次见form表单写成format的
心随自在飞 2015-11-03
  • 打赏
  • 举报
回复
设段点,debug模式! 一步一步慢慢走!
朗晴 2015-11-03
  • 打赏
  • 举报
回复
多看多练 加油。

81,122

社区成员

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

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