java中servlet的问题
我有段代码看不太懂请帮忙解释一下,最后每一句都解释一下??importjava.io.IOException;importjava.io.PrintWriter;impo...
我有段代码 看不太懂 请帮忙解释一下,最后每一句都解释一下??
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorld2 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
resp.setContentType("text/html;charset=gb2312");
try {
req.setCharacterEncoding("GBK");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
PrintWriter out = null;
try {
out = resp.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
String title = "Reading All Request Parameters";
out.println("<html>" +
"<head><title>读取所有参数</title></head>"
+ "<BODY BGCOLOR='#FDF5E6'>\n"
+ "<H1 ALIGN=CENTER>" + title
+ "</H1>\n" +
"<TABLE BORDER=1 ALIGN=CENTER>\n"
+ "<TR BGCOLOR=\"#FFAD00\">\n"
+ "<TH>Parameter Name<TH>Parameter Value(s)");
Enumeration paramNames = req.getParameterNames();//获取此列表中所有名称的枚举。
while (paramNames.hasMoreElements())
{
String paramName = (String) paramNames.nextElement();
out.print("<TR><TD>" + paramName + "\n<TD>");
String[] paramValues = req.getParameterValues(paramName);
if (paramValues.length == 1)
{
String paramValue = paramValues[0];
if (paramValue.length() == 0)
out.println("<I>No Value</I>");
else
out.println(paramValue);
}
else
{
out.println("<UL>");
for (int i = 0; i < paramValues.length; i++)
{
out.println("<LI>" + paramValues[i]);
}
out.println("</UL>");
}
}
out.println("</TABLE>\n</BODY></HTML>");
}
} 展开
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorld2 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
resp.setContentType("text/html;charset=gb2312");
try {
req.setCharacterEncoding("GBK");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
PrintWriter out = null;
try {
out = resp.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
String title = "Reading All Request Parameters";
out.println("<html>" +
"<head><title>读取所有参数</title></head>"
+ "<BODY BGCOLOR='#FDF5E6'>\n"
+ "<H1 ALIGN=CENTER>" + title
+ "</H1>\n" +
"<TABLE BORDER=1 ALIGN=CENTER>\n"
+ "<TR BGCOLOR=\"#FFAD00\">\n"
+ "<TH>Parameter Name<TH>Parameter Value(s)");
Enumeration paramNames = req.getParameterNames();//获取此列表中所有名称的枚举。
while (paramNames.hasMoreElements())
{
String paramName = (String) paramNames.nextElement();
out.print("<TR><TD>" + paramName + "\n<TD>");
String[] paramValues = req.getParameterValues(paramName);
if (paramValues.length == 1)
{
String paramValue = paramValues[0];
if (paramValue.length() == 0)
out.println("<I>No Value</I>");
else
out.println(paramValue);
}
else
{
out.println("<UL>");
for (int i = 0; i < paramValues.length; i++)
{
out.println("<LI>" + paramValues[i]);
}
out.println("</UL>");
}
}
out.println("</TABLE>\n</BODY></HTML>");
}
} 展开
4个回答
展开全部
前面的IMPORT就不说了,都是必须的。下面是注释:
public class HelloWorld2 extends HttpServlet {
// 可有可无
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
// 设置reponse内容编码为GB2312
resp.setContentType("text/html;charset=gb2312");
try {
// 设置解析request所用的编码方式
req.setCharacterEncoding("GBK");
} catch (UnsupportedEncodingException e1) {
// 如果异常打印call stack
e1.printStackTrace();
}
// 声明并初始化PrintWriter变量
PrintWriter out = null;
try {
// 从reponse里得到Writer
out = resp.getWriter();
} catch (IOException e) {
// 如果异常打印call stack
e.printStackTrace();
}
// 声明并初始化String变量
String title = "Reading All Request Parameters";
// 输出html内容
out.println("<html>" +
"<head><title>读取所有参数</title></head>"
+ "<BODY BGCOLOR='#FDF5E6'>\n"
+ "<H1 ALIGN=CENTER>" + title
+ "</H1>\n" +
"<TABLE BORDER=1 ALIGN=CENTER>\n"
+ "<TR BGCOLOR=\"#FFAD00\">\n"
+ "<TH>Parameter Name<TH>Parameter Value(s)");
Enumeration paramNames = req.getParameterNames();//获取此列表中所有名称的枚举。
// 循环所有参数
while (paramNames.hasMoreElements())
{
// 得到当前参数
String paramName = (String) paramNames.nextElement();
// 打印参数名称
out.print("<TR><TD>" + paramName + "\n<TD>");
// 取得名称所对应的全部值
String[] paramValues = req.getParameterValues(paramName);
// 如果该名称值对应一个值
if (paramValues.length == 1)
{
// 得到该值
String paramValue = paramValues[0];
// 如果值长多为零,打印‘No Value’
if (paramValue.length() == 0)
out.println("<I>No Value</I>");
else
// 如果值不等于零,打印内容
out.println(paramValue);
}
// 如果该名称值对应多于一个值
else
{
// 打印UL标签
out.println("<UL>");
// 循环打印参数所对应的所有值
for (int i = 0; i < paramValues.length; i++)
{
out.println("<LI>" + paramValues[i]);
}
// 打印UL结束标签
out.println("</UL>");
}
}
// 打印HTML结束标签
out.println("</TABLE>\n</BODY></HTML>");
}
}
public class HelloWorld2 extends HttpServlet {
// 可有可无
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
// 设置reponse内容编码为GB2312
resp.setContentType("text/html;charset=gb2312");
try {
// 设置解析request所用的编码方式
req.setCharacterEncoding("GBK");
} catch (UnsupportedEncodingException e1) {
// 如果异常打印call stack
e1.printStackTrace();
}
// 声明并初始化PrintWriter变量
PrintWriter out = null;
try {
// 从reponse里得到Writer
out = resp.getWriter();
} catch (IOException e) {
// 如果异常打印call stack
e.printStackTrace();
}
// 声明并初始化String变量
String title = "Reading All Request Parameters";
// 输出html内容
out.println("<html>" +
"<head><title>读取所有参数</title></head>"
+ "<BODY BGCOLOR='#FDF5E6'>\n"
+ "<H1 ALIGN=CENTER>" + title
+ "</H1>\n" +
"<TABLE BORDER=1 ALIGN=CENTER>\n"
+ "<TR BGCOLOR=\"#FFAD00\">\n"
+ "<TH>Parameter Name<TH>Parameter Value(s)");
Enumeration paramNames = req.getParameterNames();//获取此列表中所有名称的枚举。
// 循环所有参数
while (paramNames.hasMoreElements())
{
// 得到当前参数
String paramName = (String) paramNames.nextElement();
// 打印参数名称
out.print("<TR><TD>" + paramName + "\n<TD>");
// 取得名称所对应的全部值
String[] paramValues = req.getParameterValues(paramName);
// 如果该名称值对应一个值
if (paramValues.length == 1)
{
// 得到该值
String paramValue = paramValues[0];
// 如果值长多为零,打印‘No Value’
if (paramValue.length() == 0)
out.println("<I>No Value</I>");
else
// 如果值不等于零,打印内容
out.println(paramValue);
}
// 如果该名称值对应多于一个值
else
{
// 打印UL标签
out.println("<UL>");
// 循环打印参数所对应的所有值
for (int i = 0; i < paramValues.length; i++)
{
out.println("<LI>" + paramValues[i]);
}
// 打印UL结束标签
out.println("</UL>");
}
}
// 打印HTML结束标签
out.println("</TABLE>\n</BODY></HTML>");
}
}
展开全部
public class HelloWorld2 extends HttpServlet {
private static final long serialVersionUID = 1L;//设置一个long型常量
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {//如果页面的form表单是用post方法发送信息,会执行这个方法doPost
resp.setContentType("text/html;charset=gb2312");
try {
req.setCharacterEncoding("GBK");//设置编码类型
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();//如果上一句有一场,会打印出异常信息
}
PrintWriter out = null;
try {
out = resp.getWriter();//out.println中的内容会输出到浏览器中
} catch (IOException e) {
e.printStackTrace();
}
String title = "Reading All Request Parameters";//初始化一个字符串
out.println("<html>" +
"<head><title>读取所有参数</title></head>"//设置输出网页标题
+ "<BODY BGCOLOR='#FDF5E6'>\n" //网页格式
+ "<H1 ALIGN=CENTER>" + title
+ "</H1>\n" +
"<TABLE BORDER=1 ALIGN=CENTER>\n"
+ "<TR BGCOLOR=\"#FFAD00\">\n"
+ "<TH>Parameter Name<TH>Parameter Value(s)");
Enumeration paramNames = req.getParameterNames();//获取此列表中所有名称的枚举。
while (paramNames.hasMoreElements()) //循环得到request中的信息
{
String paramName = (String) paramNames.nextElement();
out.print("<TR><TD>" + paramName + "\n<TD>");//在页面中的表格中打印出来
String[] paramValues = req.getParameterValues(paramName);
if (paramValues.length == 1)
{
String paramValue = paramValues[0];
if (paramValue.length() == 0)
out.println("<I>No Value</I>");
else
out.println(paramValue);
}
else
{
out.println("<UL>");
for (int i = 0; i < paramValues.length; i++)
{
out.println("<LI>" + paramValues[i]);
}
out.println("</UL>");
}
}
out.println("</TABLE>\n</BODY></HTML>");
}
}
private static final long serialVersionUID = 1L;//设置一个long型常量
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {//如果页面的form表单是用post方法发送信息,会执行这个方法doPost
resp.setContentType("text/html;charset=gb2312");
try {
req.setCharacterEncoding("GBK");//设置编码类型
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();//如果上一句有一场,会打印出异常信息
}
PrintWriter out = null;
try {
out = resp.getWriter();//out.println中的内容会输出到浏览器中
} catch (IOException e) {
e.printStackTrace();
}
String title = "Reading All Request Parameters";//初始化一个字符串
out.println("<html>" +
"<head><title>读取所有参数</title></head>"//设置输出网页标题
+ "<BODY BGCOLOR='#FDF5E6'>\n" //网页格式
+ "<H1 ALIGN=CENTER>" + title
+ "</H1>\n" +
"<TABLE BORDER=1 ALIGN=CENTER>\n"
+ "<TR BGCOLOR=\"#FFAD00\">\n"
+ "<TH>Parameter Name<TH>Parameter Value(s)");
Enumeration paramNames = req.getParameterNames();//获取此列表中所有名称的枚举。
while (paramNames.hasMoreElements()) //循环得到request中的信息
{
String paramName = (String) paramNames.nextElement();
out.print("<TR><TD>" + paramName + "\n<TD>");//在页面中的表格中打印出来
String[] paramValues = req.getParameterValues(paramName);
if (paramValues.length == 1)
{
String paramValue = paramValues[0];
if (paramValue.length() == 0)
out.println("<I>No Value</I>");
else
out.println(paramValue);
}
else
{
out.println("<UL>");
for (int i = 0; i < paramValues.length; i++)
{
out.println("<LI>" + paramValues[i]);
}
out.println("</UL>");
}
}
out.println("</TABLE>\n</BODY></HTML>");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorld2 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
resp.setContentType("text/html;charset=gb2312");
//设置输出的编码
try {
req.setCharacterEncoding("GBK");
//接收的编码
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
PrintWriter out = null;
try {
out = resp.getWriter();//out对象
} catch (IOException e) {
e.printStackTrace();
}
String title = "Reading All Request Parameters";
//以下为输出的html
out.println("<html>" +
"<head><title>读取所有参数</title></head>"
+ "<BODY BGCOLOR='#FDF5E6'>\n"
+ "<H1 ALIGN=CENTER>" + title
+ "</H1>\n" +
"<TABLE BORDER=1 ALIGN=CENTER>\n"
+ "<TR BGCOLOR=\"#FFAD00\">\n"
+ "<TH>Parameter Name<TH>Parameter Value(s)");
Enumeration paramNames = req.getParameterNames();//获取此列表中所有名称的枚举。获取request中的数据
while (paramNames.hasMoreElements()) //当有值时
{//遍历其中的key和value
String paramName = (String) paramNames.nextElement();
out.print("<TR><TD>" + paramName + "\n<TD>");
String[] paramValues = req.getParameterValues(paramName);
if (paramValues.length == 1)
{
String paramValue = paramValues[0];
if (paramValue.length() == 0)//如果没有,输出novalue
out.println("<I>No Value</I>");
else
out.println(paramValue);
}
else
{
out.println("<UL>");//这都是输出一些html代码
for (int i = 0; i < paramValues.length; i++)
{
out.println("<LI>" + paramValues[i]);
}
out.println("</UL>");
}
}
out.println("</TABLE>\n</BODY></HTML>");
}
}
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorld2 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
resp.setContentType("text/html;charset=gb2312");
//设置输出的编码
try {
req.setCharacterEncoding("GBK");
//接收的编码
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
PrintWriter out = null;
try {
out = resp.getWriter();//out对象
} catch (IOException e) {
e.printStackTrace();
}
String title = "Reading All Request Parameters";
//以下为输出的html
out.println("<html>" +
"<head><title>读取所有参数</title></head>"
+ "<BODY BGCOLOR='#FDF5E6'>\n"
+ "<H1 ALIGN=CENTER>" + title
+ "</H1>\n" +
"<TABLE BORDER=1 ALIGN=CENTER>\n"
+ "<TR BGCOLOR=\"#FFAD00\">\n"
+ "<TH>Parameter Name<TH>Parameter Value(s)");
Enumeration paramNames = req.getParameterNames();//获取此列表中所有名称的枚举。获取request中的数据
while (paramNames.hasMoreElements()) //当有值时
{//遍历其中的key和value
String paramName = (String) paramNames.nextElement();
out.print("<TR><TD>" + paramName + "\n<TD>");
String[] paramValues = req.getParameterValues(paramName);
if (paramValues.length == 1)
{
String paramValue = paramValues[0];
if (paramValue.length() == 0)//如果没有,输出novalue
out.println("<I>No Value</I>");
else
out.println(paramValue);
}
else
{
out.println("<UL>");//这都是输出一些html代码
for (int i = 0; i < paramValues.length; i++)
{
out.println("<LI>" + paramValues[i]);
}
out.println("</UL>");
}
}
out.println("</TABLE>\n</BODY></HTML>");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
首先是设置请求和相应的编码。
然后获得输出对象,
循环遍历请求的参数,并将其添加到表格中。
然后获得输出对象,
循环遍历请求的参数,并将其添加到表格中。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询