老师您好! 我都是按照视频来敲的 但是跟老师的不一样我这些地方都会报错,能帮忙看看吗?,谢谢
package com.bjsxt.servlet;
import com.bjsxt.server.Request;
import com.bjsxt.server.Response;
public class LoginServlet extends Servlet {
@Override
public void doGet(Request req, Response rep) throws Exception {
//获取请求参数
String nameString=req.getParamter("username");
String pwdString=req.getParamter("pwd");
if (this.login(name, pwd)) {
//调用响应中的构建内容的方法
rep.println(name+"登陆成功");
}else {
rep.println(name+"登陆失败,对不起,账号或密码不正确");
}
}
private boolean login(String name,String pwd){
if ("bjsxt".equals(name)&&"123".equals(pwd)) {
return true;
}
return false;
}
@Override
public void doPost(Request req, Response rep) throws Exception {
// TODO Auto-generated method stub
}
}
截图:
代码:
package com.bjsxt.server;
import java.io.IOException;
import java.net.Socket;
import com.bjsxt.servlet.Servlet;
import com.bjsxt.util.IOCloseutil;
public class Dispatcher implements Runnable{
private Request req;
private Response rep;
private Socket client;
private int code=200;//状态码
//构造方法初始化属性
public Dispatcher(Socket client){
//将局部变量的值赋给成员变量
this.client=client;
try {
req=new Request(this.client.getInputStream());
rep=new Response(this.client.getOutputStream());
} catch (IOException e) {
code=500;
return;
}
}
@Override
public void run() {
//根据不同的url创建指定的Servlet对象
//System.out.println(req.getUrl());
Servlet servlet=WebApp.getServlet(req.getUrl());
if (servlet==null) {
this.code=404;
}else{
//调用相应的Servlet中的service
try {
servlet.service(req, rep);
} catch (Exception e) {
this.code=500;
}
}
//将响应结果推送到客户机的浏览器
rep.pushToClient(code);
IOCloseutil.closeAll(client);
}
}
截图:
这个老师那里没有错 但是我这边就报错了
代码:
package com.bjsxt.server;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import org.omg.IOP.Codec;
import com.bjsxt.util.IOCloseutil;
public class Response {/*响应*/
private StringBuilder headInfo;//响应头
private StringBuilder content;//响应内容
private int length;//响应内容的长度
//流
private BufferedWriter bw;
//两个常量,换行和空格
private static final String CRLF="\r\n";//换行
private static final String BLANK=" ";//空格
//构造方法
public Response(){
headInfo=new StringBuilder();
content=new StringBuilder();
}
//带参构造方法
public Response(OutputStream os){
this();//调用本类的无参构造方法
try {
bw=new BufferedWriter(new OutputStreamWriter(os, "utf-8"));
} catch (UnsupportedEncodingException e) {
headInfo=null;
}
}
//构造正文部分
public Response print(String info){
content.append(info);
try {
length+=info.getBytes("utg=f-8").length;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return this;
}
public Response println(String info){
content.append(info).append(CRLF);
try {
length+=(info+CRLF).getBytes("utf-8").length;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return this;
}
//构造响应头
private void createHeadInfo(){
Object code;
headInfo.append("HTTP/1.1").append(BLANK).append(code).append(BLANK);
switch (code) {
case 200:
headInfo.append("OK");
break;
case 500:
headInfo.append("SERVER ERROR");
break;
default:
headInfo.append("NOT FOUND");
break;
}
headInfo.append(CRLF);
headInfo.append("Content-Type:text/html;charset=ytf-8").append(CRLF);
headInfo.append("Content-Length:"+length).append(CRLF);
headInfo.append(CRLF);
}
/**
* 推动到客户机的浏览器
* @param code
*/
public void pushToClient(int code){
if (headInfo==null) {
code=500;
}
try {
//调用本类中的构造响应头
this.createHeadInfo(code);
bw.write(headInfo.toString());
bw.write(content.toString());
bw.flush();
this.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void createHeadInfo(int code) {
// TODO Auto-generated method stub
}
public void close(){
IOCloseutil.closeAll(bw);
}
}
截图:
这个提示的是:不能打开类型 Object 的值。只允许使用可转换的 int 值或枚举常量
代码:
package com.bjsxt.server;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import com.bjsxt.servlet.Servlet;
import com.bjsxt.util.IOCloseutil;
public class Server {//服务器,用于启动和停止服务
private ServerSocket server;
private boolean isShutDown=false;//默认没有出错
public static void main(String[] args) {
Server server=new Server();//创建服务器对象
server.start();
}
public void start(){
this.start(8888);
}
public void start(int port){
try {
server=new ServerSocket(port);
this.receive();//调用接受请求信息的方法
} catch (IOException e) {
isShutDown=true;
}
}
private void receive() {
try {
while(!isShutDown){
//(1)监听
Socket client=server.accept();
//创建线程类的对象
Dispatcher dis=new Dispatcher(client);
//创建代理类并启动线程
new Thread(dis).start();
}
} catch (IOException e) {
this.stop();//关闭服务器
}
}
public void stop(){
isShutDown=true;
IOCloseutil.closeAll(server);
}
}
截图:
提示:类型 IOCloseutil 中的方法 closeAll(Closeable...)对于参数(ServerSocket)不适用
问题出现的比较多 辛苦老师帮忙解答一下,谢谢!