大家好,我被要求将旧的Java EE Web应用程序(JSP/Servlet,EJB,JPA)转换为一个现代的JSF应用程序,除了servlet之外我做的最多,
当前的servlet是:
@WebServlet(name = "StudentServlet", urlPatterns = {"/StudentServlet"})
public class StudentServlet extends HttpServlet {
@EJB
private StudentDaoLocal studentDao;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
String studentIdStr = request.getParameter("studentId");
int studentId=0;
if(studentIdStr!=null && !studentIdStr.equals("")){
studentId=Integer.parseInt(studentIdStr);
}
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String yearLevelStr = request.getParameter("yearLevel");
int yearLevel=0;
if(yearLevelStr!=null && !yearLevelStr.equals("")){
yearLevel=Integer.parseInt(yearLevelStr);
}
Student student = new Student(studentId, firstname, lastname, yearLevel);
if("Add".equalsIgnoreCase(action)){
studentDao.addStudent(student);
}else if("Edit".equalsIgnoreCase(action)){
studentDao.editStudent(student);
}else if("Delete".equalsIgnoreCase(action)){
studentDao.deleteStudent(studentId); …Run Code Online (Sandbox Code Playgroud) 我是servlets的新手,java.lang.ClassNotFoundException每当我尝试包含JSON库时,我都会在搜索web和stackoverflow时找到我发现的唯一建议是安装依赖项我已经尝试了org.json,net.sf.json每个都有其依赖项工作并给出相同的例外.
任何的想法?
我想做一个简单的post-redirect - 使用JSP.这就是我做到的.重要的Servlet是这样的:
public class PostRedirectGet extends HttpServlet {
public void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
throws ServletException, IOException {
getServletContext().getRequestDispatcher("/WEB-INF/getInformation.jsp")
.forward(httpServletRequest,httpServletResponse);
}
public void doPost(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse)
throws IOException {
String firstName = httpServletRequest.getParameter("firstName");
HttpSession httpSession = httpServletRequest.getSession();
httpSession.setAttribute("firstName",firstName);
httpServletResponse.sendRedirect(getServletContext().getContextPath()+"/getFormData");
}
}
Run Code Online (Sandbox Code Playgroud)
因此,当对此Servlet(/ index)发出get请求时,我只显示表单所在的getInformation.jsp.
表单对同一个url(/ index)发出post请求,这次调用doPost.在这里我保持firstName如下所示:
String firstName = httpServletRequest.getParameter("firstName");
Run Code Online (Sandbox Code Playgroud)
然后我将用户重定向到/ getFormData.这是负责任的servlet:
public class Get extends HttpServlet {
public void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
throws ServletException, IOException {
String firstName = (String) …Run Code Online (Sandbox Code Playgroud) 我正在创建一个测验应用程序.这里有5个jsp页面,有5个不同的quizes.如果我使用1个jsp页面和结果页面,它工作得很好...... 它从QuizPage5.jsp重定向并在ResultPage.jsp中显示结果.我需要将QuizPage1.jsp,QuizPage2.jsp,QuizPage3.jsp,QuizPage4.jsp和QuizPage5.jsp的所有结果显示到ResultPage.jsp.
我用了
在jsp1中传递值.
<form action="">
<input type="hidden" name="hidden" value="hidden">
<input type="submit" value="submit"></form>
Run Code Online (Sandbox Code Playgroud)
在jsp2中获取价值
String value=request.getParameter("hidden");
Run Code Online (Sandbox Code Playgroud)
但是,我得到了 java.lang.NullPointerException
这是我的代码..
QuizPage1.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script language="JavaScript">
function check()
{
var radio_choice = false;
for (counter = 0; counter < myform.grp.length; counter++)
{
if (myform.grp[counter].checked)
radio_choice = true;
}
if (!radio_choice)
{
alert("Please select one.")
return (false);
}
return(true);
}
</script>
</head>
<body>
<h3>Quiz No.1</h3>
Q1.Which one of the following is the Capital of India?<br><br>
<form action="QuizPage2.jsp" method="post" name="myform" …Run Code Online (Sandbox Code Playgroud) 我已经在Simple java class中实现了一个ServletContextListener.现在我已经在ServletContextListener的contextInitialized运行时调用了一个方法来执行.被调用方法的本质是它很复杂并且需要很长时间才能执行.只有一个名为index.jsp的网页是Web应用程序运行时需要在浏览器上显示的欢迎页面,但这不会显示为第一个被调用的方法执行,并且需要很长时间才能执行.
我需要欢迎页面显示和在ServletContextListener中调用的函数才能在后台执行.
这是我的ServletContextListener类..
public class Startup implements ServletContextListener
{
@Override
public void contextDestroyed(ServletContextEvent sce) {}
@Override
public void contextInitialized(ServletContextEvent sce)
{
// Do your startup work here
executeprocess();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的web.xml文件..
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.myapp.Startup</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)
请帮我.提前致谢..
我正在为注册表单编写验证servlet.表单位于.jsp文件中,并具有以下布局:
<div class="content">
<h2 class="form">Register</h2>
<form action="Register">
<p>
User name: <br />
<input type="text" name="username" /><br /> First Name: <br />
<input type="text" name="fist_name" /><br /> Last Name: <br />
<input type="text" name="last_name" /><br /> Email Address: <br />
<input type="text" name="email" /><br /> Password: <br />
<input type="password" name="password" /><br /> Retype password:
<br />
<input type="password" name="password2" /><br /> <input
type="submit" value="Register" />
</p>
</form>
<!-- end .content -->
</div>
Run Code Online (Sandbox Code Playgroud)
验证代码应检查每个字段是否满足一组约束,如果不满足,则写回jsp错误消息.
ValidationServlet.java
protected void doPost(HttpServletRequest request,
HttpServletResponse response) …Run Code Online (Sandbox Code Playgroud) 我有以下代码,
public class HttpAdapter extends HttpServlet {
private static final long serialVersionUID = 1L;
static Logger l = Logger.getLogger(HttpAdapter.class.getName());
public HttpAdapter() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
Layout ll = new SimpleLayout();
Appender a = new FileAppender();
a = new FileAppender(ll,"C:\\Users\\Vasanth\\Desktop\\JDlogs\\my.txt");
l.info(request.getRemoteHost());
l.addAppender(a);
String loc = DTO.findMyLocation();
l.info(loc);
l.info(this.getClass()+" >>>>>>>>>>>>>>task complete");
l.info(request.getRemoteHost());
}catch(Exception e){
e.printStackTrace();
}finally{
getServletContext().getRequestDispatcher("/JSP/done.jsp").forward(request, response);
}
}
//F:\\home\\WorkSpace\\Jdfront1\\webapps\\JSP\\
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
} …Run Code Online (Sandbox Code Playgroud) 我有一个HTML表单enctype="multipart/form-data"。我有一个dto它拥有所有类setter和getters。由于我要以多部分形式提交表单,getParameter()因此无法使用我使用Apache Commons BeanUtils来处理html表单字段的方法。我的servlet如下
List<FileItem> items = (List<FileItem>) new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
if (item.isFormField()) {
// Process regular form field (input type="text|radio|checkbox|etc", select, etc).
String fieldname = item.getFieldName();
String fieldvalue = item.getString();
System.out.println(fieldname);
System.out.println(fieldvalue);
// ... (do your job here)
//getters and setters
try {if((!fieldname.equals("dob"))&&(!fieldname.equals("doj"))){
BeanUtils.setProperty(teacherInfo, fieldname, fieldvalue);}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} else {
//Code for …Run Code Online (Sandbox Code Playgroud) 在下面的servlet中,我想添加内容类型和字符集编码.
public class FBOAuth extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
this.response.setContentType("application/json");
this.response.setCharacterEncoding("UTF-8");
...
Run Code Online (Sandbox Code Playgroud)
我使用以下命令编译此servlet.
$ javac -classpath json.jar FBOAuth.java
Run Code Online (Sandbox Code Playgroud)
FBOAuth.java:24: error: cannot find symbol
this.response.setContentType("application/json");
^
symbol: variable response
FBOAuth.java:25: error: cannot find symbol
this.response.setCharacterEncoding("UTF-8");
^
symbol: variable response
2 errors
Run Code Online (Sandbox Code Playgroud) 我有一个控制器(例如.MyManager),我调用组件类(bean)的方法(例如myMethod()),例如MyComponent.我有servlet,我想调用myMethod().在servlet中,我通过@Autowired注释注释了MyManager ,尽管我得到了NullPointerException.我看到了这种话题,但它对我没用.为了想象,我写了一些代码:
public class myClass extends HttpServlet {
@Autowired
private MyComponent component;
public void init(ServletConfig config) throws ServletException{
super.init(config);
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
protected void doGet(HttpServletRequest req,HttpServletResponse res) throws ... {
List<MyObject> objects =component.myMethod(); // Here is the problem, component is null
}
}
}
Run Code Online (Sandbox Code Playgroud)
我使用Spring配置文件"context.xml"并获得了bean(组件)对象,但是现在我在bean对象中注入了EntityManager有问题.现在它是null,任何人都可以帮我解决这个问题吗?还更新init()方法.
public void init(ServletConfig config) throws ServletException{
ApplicationContext con = new ClassPathXmlApplicationContext("context.xml");
component = (MyComponent) con.getBean("myBean");
}
Run Code Online (Sandbox Code Playgroud) servlets ×10
java ×8
jsp ×5
tomcat ×3
autowired ×1
eclipse ×1
file-upload ×1
java-ee ×1
javascript ×1
jsf ×1
json ×1
spring ×1
spring-mvc ×1
this ×1
web.xml ×1